本文整理汇总了Python中moments.path.Path.exists方法的典型用法代码示例。如果您正苦于以下问题:Python Path.exists方法的具体用法?Python Path.exists怎么用?Python Path.exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类moments.path.Path
的用法示例。
在下文中一共展示了Path.exists方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def main():
if len(sys.argv) > 1:
helps = ['--help', 'help', '-h']
for i in helps:
if i in sys.argv:
usage()
exit()
f1 = sys.argv[1]
if len(sys.argv) > 2:
f2 = sys.argv[2]
else:
f1_path = Path(f1)
f1_dir = f1_path.parent()
f2 = os.path.join(str(f1_dir), "summary.txt")
f2_path = Path(f2)
if not f2_path.exists():
print("Saving output to: %s" % f2)
else:
print("Warning: %s exists!" % f2)
exit()
create_summary(f1, f2)
else:
usage()
exit()
示例2: process_files
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def process_files(source_list, translate=None, action="copy", m3u_dest="temp.txt"):
"""
copy *only* the files referenced in a source_list to a new loacation
"""
result = ''
#j = Journal()
#j.from_file(journal)
#j = load_journal(journal)
#m = MediaList()
#m.from_journal(j, local_path='/c')
#sources = Sources()
sl = Path(source_list)
assert sl.exists()
converter = Converter()
if sl.extension == ".m3u":
print("M3U!")
sources = converter.from_m3u(source_list)
elif sl.extension == ".txt":
sources = converter.from_journal(source_list)
else:
print("UNKNOWN EXTENSION: %s" % sl.extension)
new_sources = Sources()
counter = 0
for i in sources:
#print i
#if re.search('\.mp3', i.path):
if os.path.exists(str(i.path)):
destination = make_destination(i.path, translate)
#print "SOURCE: %s" % i
print("DEST: %s" % destination)
if action == "copy":
print("Copy %03d: %s" % (counter, i.path))
copy_file(i.path, destination)
if action == "m3u":
new_sources.append(Source(destination))
else:
print("COULD NOT FIND FILE: %s" % i.path)
counter += 1
if action == "m3u":
#print len(new_sources)
m3u = converter.to_m3u(new_sources, verify=False)
#print m3u
print("SAVING M3U TO: %s" % m3u_dest)
f = open(m3u_dest, 'w')
f.write(m3u)
f.close()
示例3: play
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def play(source='', start=0):
"""
source should be either a path or a string
if it's just a string, then should try to identify the correct path to the media
(search a library of local data?)
"""
process = None
p = Path(source)
if not p.exists():
#TODO
#could search here
#but for now...
print("Could not find: %s" % source)
else:
process = vlc(source, start)
return process
示例4: reset_images
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def reset_images(source):
print("Looking in: %s" % source)
path = Path(source)
if path.exists() and path.type() == "Directory":
d = path.load()
d.scan_filetypes()
#print d.images
for image in d.images:
#print image
i = image.load()
print("old: %s" % i.datetime())
i.datetime_exif()
i = image.load()
print("new: %s" % i.datetime())
print()
#print d.directories
for sub_d in d.directories:
reset_images(sub_d)
else:
print("Couldn't find path: %s" % source)
exit()
示例5: check_dates
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def check_dates(sources):
"""
go through all sources and look for the days covered
this is useful to check for media from
devices that don't keep an accurate clock
related to group_by_day, but not processing any batches here
"""
dates = []
for source in sources:
p = Path(source)
if p.exists():
d = p.load()
#print(d)
#print(type(d))
d.sort_by_date()
for fpath in d.files:
#print f.name
f = fpath.load()
if not f.date() in dates:
dates.append(f.date())
return dates
示例6: save
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def save(relative=''):
# debug:
# print(dir(bottle.request.forms))
# print("Keys: %s" % (bottle.request.forms.keys()))
# print("Values: %s" % (bottle.request.forms.values()))
global path_root
if re.match('~', relative):
relative = os.path.expanduser(relative)
if not relative:
# could set a default here if it is desireable
print("NO DESTINATION SENT!")
elif not re.match('/', relative):
relative = path_root + relative
# destination = Path(relative, relative_prefix=path_root)
# now check if the destination is a directory...
# in that case, create a sortable.list name in the directory
if os.path.isdir(relative):
print("Relative directory passed in:", relative)
path = Path(relative)
print("Path loaded:", path)
name = path.name + ".list"
destination = os.path.join(relative, name)
# TODO:
# something recursive can happen here:
# having difficulty replicating...
# should work though!
# destination = path.sortable_list_path()
print("Destination:", destination)
log_name = path.name + ".list.log"
log_destination = os.path.join(relative, log_name)
else:
destination = relative
log_destination = relative + ".log"
# print(destination)
# gets a string
# could be json or text / list
content = bottle.request.forms.get('content')
# print("CONTENT:")
# print(content)
# print()
save_as = bottle.request.forms.get('format')
if destination:
if save_as in ["list"]:
dest_file = open(destination, 'w')
# print("opened: ", destination)
# print("writing (raw): ", content)
dest_file.write(content)
dest_file.close()
print("saved content to: ", destination)
# TODO:
# check the .list.log file to see if today's entry already exists
log_path = Path(log_destination)
print("Log path: ", log_path)
if log_path.exists():
j = log_path.load_journal()
else:
j = log_path.load_journal(create=True)
now = Timestamp()
now.compact(accuracy='day')
entries = j.range(now)
print(entries)
if len(entries):
print("already had today logged")
else:
j.make(content, created=now)
j.save(log_path)
print("adding new entry")
# this should be sufficient
return("Success!")
else:
return("Unknown format: " + str(save_as))
## elif save_as == "json":
## #could also do something like:
## ordered_list = json.loads(content)
## save_json(destination, ordered_list)
## #but that seems like the same thing as above
# if save() is being called via ajax,
# redirecting here only causes extra load on the server
# redirect("/text" + relative)
# redirect("/path" + relative)
#.........这里部分代码省略.........
示例7: sort
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def sort(relative=''):
"""
accept a path to a moment log and enable sorting on the items
using jquery ui for a drag and drop interface
"""
global path_root
if re.match('~', relative):
relative = os.path.expanduser(relative)
if not re.match('/', relative):
relative = path_root + relative
#set some defaults here...
#if they've been changed, this will get over written on load
groups = { "all":[], "edit":[], "slide1":[], "slide2":[], "slide3":[], "slide4":[], "slide5":[], "slide6":[], "slide7":[], "slide8":[], "slide9":[], }
tab_order = ['all', 'edit', "slide1", "slide2", "slide3", "slide4", "slide5", "slide6", "slide7", "slide8", "slide9"]
path = Path(relative, relative_prefix=path_root)
print(path)
if path.exists() and path.type() == "Directory":
response = "Error: need a file name to store the meta data in<br>"
response = "You supplied a directory path: %s<br>" % path
return response
else:
parent_directory = path.parent()
if path.extension == ".txt":
#create a text journal if we don't have one
if not path.exists():
#convert images to journal
#print "PARENT: %s" % parent_directory
directory = parent_directory.load()
#print "directory: %s, of type: %s" % (directory, type(directory))
directory.create_journal(journal=path.filename)
#journal = path.load_journal(create=True)
journal = path.load_journal()
items = []
for e in journal.entries():
new_p = os.path.join(str(parent_directory), e.data.strip())
#print new_p
p = Path(new_p)
#print p.exists()
items.append(p)
#initial version of groups:
destination = Path(relative)
destination.extension = '.json'
groups['all'] = items
elif path.extension == ".json":
#we can make the initial version here...
#skip the generation of a moments log step
if not path.exists():
directory = parent_directory.load()
#print "directory: %s, of type: %s" % (directory, type(directory))
directory.sort_by_date()
directory.scan_filetypes()
groups['all'] = directory.images
else:
loaded = load_groups(str(path))
#template expects all items in groups to be Path objects.
#do that now
groups = {}
for key, value in list(loaded.items()):
groups[key] = []
for v in value:
groups[key].append(Path(v))
destination = Path(relative)
else:
#dunno!
print("UNKNOWN FILE TYPE: %s" % relative)
groups = {}
destination = None
#clean up tab_order as needed
for key in list(groups.keys()):
if not key in tab_order:
tab_order.append(key)
for item in tab_order[:]:
if item not in list(groups.keys()):
tab_order.remove(item)
print(tab_order)
#return template('sort', path=path, items=items)
return template('sort', path=path, groups=groups, destination=destination, tab_order=tab_order)
示例8: make_module
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def make_module(module_name, short_description):
here = Path('.')
print(here)
#customize:
version = "0.1.0"
author = "Charles Brandt"
email = "[email protected]"
now = Timestamp()
#should be passed in as an argument now
#module_name = "Probe"
module_low = module_name.lower()
print(module_low)
if (here.name != module_name) and (here.name != module_low):
print("current directory name does not equal module name:")
print("%s != %s" % (here.name, module_name))
exit()
#few different good options here:
#url = 'http://pypi.python.org/pypi/%s/' % module_name
#url = 'http://packages.python.org/%s/' % module_name
url = 'https://bitbucket.org/cbrandt/%s/' % module_low
#short_description = "Objects to help process and sort collections and their contents."
module_path = Path(os.path.join(str(here), module_low))
if not module_path.exists():
module_path.create()
print("Created: %s" % module_path)
module_init = Path(os.path.join(str(module_path), "__init__.py"))
if not module_init.exists():
module_init.create()
print("Created: %s" % module_init)
#this is the other important thing to customize:
setup = """from setuptools import setup
#haven't gotten this to work yet:
#from distutils.core import setup
setup(
name='%s',
version='%s',
author='%s',
author_email='%s',
packages=['%s'],
scripts=[],
url='%s',
license='LICENSE.txt',
description='%s',
long_description=open('README.txt').read(),
install_requires=[
],
)
""" % (module_name, version, author, email, module_low, url, short_description)
f = open("setup.py", 'w')
f.write(setup)
f.close()
readme = "README.txt"
if not os.path.exists(readme):
f = open(readme, 'w')
f.write("""==============
{module_name}
==============
{short_description}
DOCS
---------
Complete documentation can be found in:
docs/
To generate the documentation, you will need Sphinx:
sudo easy_install sphinx
cd docs/
sphinx-build -b html . ./_build/html/
or
make html
INSTALL
----------
see docs/installation.txt
and
docs/_build/html/installation.html
CONTRIBUTORS
-----------------
#.........这里部分代码省略.........
示例9: download_with_browser_helper
# 需要导入模块: from moments.path import Path [as 别名]
# 或者: from moments.path.Path import exists [as 别名]
def download_with_browser_helper(driver, link, browser_root='/Users/user/Downloads/', filename=None, ping_page=None, ping_interval=None):
"""
specialized download routine to use the browser to save files
then poll for the file to be complete before moving on
driver is an instance of a selenium/webdriver instance running a browser
if the download stalls, move on after a threshold is met
this does not handle moving the file
only returns true or false depending on if the file needed to be skipped
because it stalled out.
browser_root is the default location that browser is configured
to download to
browser should be set to automatically download files
of the type you are downloading in order to automate the process
"""
skipped = False
#*2011.12.30 13:28:09
#sometimes it's not possible to know the filename
#until after you start the download
#(that is, it only shows up in the directory.)
#
#to work around that,
#get the current files in the directory before starting the download
#then look after and see what the difference is
pre_files = os.listdir(browser_root)
#this kicks off the actual download:
driver.get(link)
#make sure there has been time for .part to be created
time.sleep(30)
post_files = os.listdir(browser_root)
new_files = []
for f in post_files:
if f not in pre_files:
new_files.append(f)
print("found these new files: %s" % new_files)
assert len(new_files) < 3 and len(new_files) > 0
for f in new_files:
if not re.search('\.part', f):
filename = f
download_dest = os.path.join(browser_root, filename)
dpart = download_dest + '.part'
#start = Timestamp()
stall_start = None
stalled = False
size = 0
new_size = -1
ping_start = Timestamp()
dpartp = Path(dpart)
#there is a chance it finished downloading if the file was small
if dpartp.exists():
dpartf = dpartp.load()
while dpartp.exists():
#do this before the sleep to make sure it's still there
new_size = dpartf.check_size()
time.sleep(10)
now = Timestamp()
if ping_page and ping_interval:
if now.datetime > ping_start.future(minutes=ping_interval).datetime:
print("time to ping! %s, %s" % (now, ping_page))
driver.get(ping_page)
ping_start = Timestamp()
#if the size has changed, something is happening
#not stalled
if new_size != size:
if stalled:
now = Timestamp()
print("download resumed: %s" % now)
stalled = False
size = new_size
#could just be slow to download, but want to at least check
else:
#we've already set it before
if stalled:
#check if stall_start was 25 minutes ago
if now.datetime > stall_start.future(minutes=25).datetime:
print("stall threshold met. deleting: %s" % dest)
dpartp.remove()
dpath.remove()
#if we get here, we know it didn't work
skipped = True
#log skips to make sure we go back:
#.........这里部分代码省略.........