本文整理汇总了Python中posixpath.exists函数的典型用法代码示例。如果您正苦于以下问题:Python exists函数的具体用法?Python exists怎么用?Python exists使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了exists函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copy_license_notice_files
def copy_license_notice_files(fields, base_dir, license_notice_text_location, afp):
lic_name = u''
for key, value in fields:
if key == u'license_file' or key == u'notice_file':
lic_name = value
from_lic_path = posixpath.join(to_posix(license_notice_text_location), lic_name)
about_file_dir = dirname(to_posix(afp)).lstrip('/')
to_lic_path = posixpath.join(to_posix(base_dir), about_file_dir)
if on_windows:
from_lic_path = add_unc(from_lic_path)
to_lic_path = add_unc(to_lic_path)
# Strip the white spaces
from_lic_path = from_lic_path.strip()
to_lic_path = to_lic_path.strip()
# Errors will be captured when doing the validation
if not posixpath.exists(from_lic_path):
continue
if not posixpath.exists(to_lic_path):
os.makedirs(to_lic_path)
try:
shutil.copy2(from_lic_path, to_lic_path)
except Exception as e:
print(repr(e))
print('Cannot copy file at %(from_lic_path)r.' % locals())
示例2: send_head
def send_head(self):
path = self.translate_path(self.path)
f = None
if path is None:
self.send_error(404, "File not found")
return None
if path is '/':
return self.list_directory(path)
if os.path.isdir(path):
# not endswidth /
if not self.path.endswith('/'):
# redirect browser - doing basically what apache does
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
# looking for index.html or index.htm
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)
# If there's no extension and the file doesn't exist,
# see if the file plus the default extension exists.
if (SublimeServerHandler.defaultExtension and
not posixpath.splitext(path)[1] and
not posixpath.exists(path) and
posixpath.exists(path + SublimeServerHandler.defaultExtension)):
path += SublimeServerHandler.defaultExtension
ctype = self.guess_type(path)
try:
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found")
return None
try:
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
self.send_header(
"Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return f
except:
f.close()
raise
示例3: compile_templates
def compile_templates(root, output, minify, input_encoding='utf-8', watch=True):
"""Compile all templates in root or root's subfolders."""
root = posixpath.normpath(root)
root_len = len(root)
output = posixpath.normpath(output)
for dirpath, dirnames, filenames in os.walk(root):
dirpath = posixpath.normpath(dirpath)
if posixpath.basename(dirpath).startswith('.'): continue
filenames = [f for f in filenames if not f.startswith('.') and not f.endswith('~') and not f.endswith('.py') and not f.endswith('.pyc')]
outdir = posixpath.join(output , dirpath[root_len:])
if not posixpath.exists(outdir):
os.makedirs(outdir)
if not posixpath.exists(posixpath.join(outdir, '__init__.py')):
out = open(posixpath.join(outdir, '__init__.py'), 'w')
out.close()
for f in filenames:
path = posixpath.join(dirpath, f).replace('\\','/')
outfile = posixpath.join(outdir, f.replace('.','_')+'.py')
filemtime = os.stat(path)[stat.ST_MTIME]
if not exists(outfile) or os.stat(outfile)[stat.ST_MTIME] < filemtime:
uri = path[root_len+1:]
print 'compiling', uri
text = file(path).read()
if minify:
text = minify_js_in_html(uri, text)
t = mako.template.Template(text=text, filename=path, uri=uri, input_encoding=input_encoding)
out = open(outfile, 'w')
out.write( t.code)
out.close()
if watch:
watch_folder_for_changes(dirpath, minify)
示例4: safe_rename
def safe_rename(old, new):
if old != new:
if not posixpath.exists(old):
raise Exception, "Old path does not exist: %s" % old
if posixpath.exists(new):
raise Exception, "New path exists already: %s" % new
os.rename(old, new)
示例5: test_exists
def test_exists(self):
self.assertIs(posixpath.exists(test_support.TESTFN), False)
f = open(test_support.TESTFN, "wb")
try:
f.write("foo")
f.close()
self.assertIs(posixpath.exists(test_support.TESTFN), True)
self.assertIs(posixpath.lexists(test_support.TESTFN), True)
finally:
if not f.close():
f.close()
self.assertRaises(TypeError, posixpath.exists)
示例6: safe_make_fifo
def safe_make_fifo(path, mode=0666):
if posixpath.exists(path):
mode = os.stat(path)[ST_MODE]
if not S_ISFIFO(mode):
raise Exception, "Path is not a FIFO: %s" % path
else:
os.mkfifo(path, mode)
示例7: safe_change_mode
def safe_change_mode(path, mode):
if not posixpath.exists(path):
raise Exception, "Path does not exist: %s" % path
old_mode = os.stat(path)[ST_MODE]
if mode != S_IMODE(old_mode):
os.chmod(path, mode)
示例8: test_islink
def test_islink(self):
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
f = open(test_support.TESTFN + "1", "wb")
try:
f.write("foo")
f.close()
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
if hasattr(os, "symlink"):
os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
os.remove(test_support.TESTFN + "1")
self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
finally:
if not f.close():
f.close()
try:
os.remove(test_support.TESTFN + "1")
except os.error:
pass
try:
os.remove(test_support.TESTFN + "2")
except os.error:
pass
self.assertRaises(TypeError, posixpath.islink)
示例9: write
def write(self):
import posixpath
if os.environ.has_key('CDDB_WRITE_DIR'):
dir = os.environ['CDDB_WRITE_DIR']
else:
dir = os.environ['HOME'] + '/' + _cddbrc
file = dir + '/' + self.id + '.rdb'
if posixpath.exists(file):
# make backup copy
posix.rename(file, file + '~')
f = open(file, 'w')
f.write('album.title:\t' + self.title + '\n')
f.write('album.artist:\t' + self.artist + '\n')
f.write('album.toc:\t' + self.toc + '\n')
for note in self.notes:
f.write('album.notes:\t' + note + '\n')
prevpref = None
for i in range(1, len(self.track)):
if self.trackartist[i]:
f.write('track'+`i`+'.artist:\t'+self.trackartist[i]+'\n')
track = self.track[i]
try:
off = string.index(track, ',')
except string.index_error:
prevpref = None
else:
if prevpref and track[:off] == prevpref:
track = track[off:]
else:
prevpref = track[:off]
f.write('track' + `i` + '.title:\t' + track + '\n')
f.close()
示例10: renderArea
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
"""
"""
if self.mapnik is None:
self.mapnik = mapnik.Map(0, 0)
if exists(self.mapfile):
mapnik.load_map(self.mapnik, str(self.mapfile))
else:
handle, filename = mkstemp()
os.write(handle, urlopen(self.mapfile).read())
os.close(handle)
mapnik.load_map(self.mapnik, filename)
os.unlink(filename)
self.mapnik.width = width
self.mapnik.height = height
self.mapnik.zoom_to_box(mapnik.Envelope(xmin, ymin, xmax, ymax))
img = mapnik.Image(width, height)
mapnik.render(self.mapnik, img)
img = Image.fromstring("RGBA", (width, height), img.tostring())
return img
示例11: test
def test():
#----------------------------------------------------------------------------
##TODO: a more serious test with distinct processes !
print('Testing glock.py...')
# unfortunately can't test inter-process lock here!
lockName = 'myFirstLock'
l = GlobalLock(lockName)
if not _windows:
assert posixpath.exists(lockName)
l.acquire()
l.acquire() # reentrant lock, must not block
l.release()
l.release()
try: l.release()
except NotOwner: pass
else: raise Exception('should have raised a NotOwner exception')
# Check that <> threads of same process do block:
import threading, time
thread = threading.Thread(target=threadMain, args=(l,))
print('main: locking...', end=' ')
l.acquire()
print(' done.')
thread.start()
time.sleep(3)
print('\nmain: unlocking...', end=' ')
l.release()
print(' done.')
time.sleep(0.1)
print('=> Test of glock.py passed.')
return l
示例12: __init__
def __init__(self, fpath, lockInitially=False, logger=None):
''' Creates (or opens) a global lock.
@param fpath: Path of the file used as lock target. This is also
the global id of the lock. The file will be created
if non existent.
@param lockInitially: if True locks initially.
@param logger: an optional logger object.
'''
self.logger = logger
self.fpath = fpath
if posixpath.exists(fpath):
self.previous_lockfile_present = True
else:
self.previous_lockfile_present = False
if _windows:
self.name = string.replace(fpath, '\\', '_')
self.mutex = win32event.CreateMutex(None, lockInitially, self.name)
else: # Unix
self.name = fpath
self.flock = open(fpath, 'w')
self.fdlock = self.flock.fileno()
self.threadLock = threading.RLock()
if lockInitially:
self.acquire()
示例13: main
def main(args):
sections = SplitArgsIntoSections(args[1:])
assert len(sections) == 3
command = sections[0]
outputs = sections[1]
inputs = sections[2]
assert len(inputs) == len(outputs)
# print 'command: %s' % command
# print 'inputs: %s' % inputs
# print 'outputs: %s' % outputs
for i in range(0, len(inputs)):
# make sure output directory exists
out_dir = posixpath.dirname(outputs[i])
if not posixpath.exists(out_dir):
posixpath.makedirs(out_dir)
# redirect stdout to the output file
out_file = open(outputs[i], 'w')
# build up the command
subproc_cmd = [command[0]]
subproc_cmd.append(inputs[i])
subproc_cmd += command[1:]
# fork and exec
returnCode = subprocess.call(subproc_cmd, stdout=out_file)
# clean up output file
out_file.close()
# make sure it worked
assert returnCode == 0
return 0
示例14: printindex
def printindex(outfilename,headfilename,levels,titles,tables):
# Read in the header file
headbuf = ''
if posixpath.exists(headfilename) :
try:
fd = open(headfilename,'r')
except:
print 'Error reading file',headfilename
exit()
headbuf = fd.read()
fd.close()
else:
print 'Header file \'' + headfilename + '\' does not exist'
# Now open the output file.
try:
fd = open(outfilename,'w')
except:
print 'Error writing to file',outfilename
exit()
# Add the HTML Header info here.
fd.write(headbuf)
# Add some HTML separators
fd.write('\n<P>\n')
fd.write('<TABLE>\n')
for i in range(len(levels)):
level = levels[i]
title = titles[i]
if len(tables[i]) == 0:
# If no functions in 'None' category, then don't print
# this category.
if level == 'none':
continue
else:
# If no functions in any other category, then print
# the header saying no functions in this cagetory.
fd.write('</TR><TD WIDTH=250 COLSPAN="3">')
fd.write('<B>' + 'No ' + level +' routines' + '</B>')
fd.write('</TD></TR>\n')
continue
fd.write('</TR><TD WIDTH=250 COLSPAN="3">')
#fd.write('<B>' + upper(title[0])+title[1:] + '</B>')
fd.write('<B>' + title + '</B>')
fd.write('</TD></TR>\n')
# Now make the entries in the table column oriented
tables[i] = maketranspose(tables[i],3)
for filename in tables[i]:
path,name = posixpath.split(filename)
func_name,ext = posixpath.splitext(name)
mesg = ' <TD WIDTH=250><A HREF="'+ './' + name + '">' + \
func_name + '</A></TD>\n'
fd.write(mesg)
if tables[i].index(filename) % 3 == 2 : fd.write('<TR>\n')
fd.write('</TABLE>\n')
# Add HTML tail info here
fd.write('<BR><A HREF="../index.html"><IMG SRC="../up.gif">Table of Contents</A>\n')
fd.close()
示例15: __init__
def __init__(self, filename=None, name=None, robot=None, brain=None, echo=1, mode="w"):
"""
Pass in robot and brain so that we can query them (and maybe make
copies and query them on occation).
If echo is True, then it will echo the log file to the terminal
"""
self.open = 1
timestamp = self.timestamp()
if filename == None:
filename = timestamp + ".log"
while posixpath.exists(filename):
timestamp = self.timestamp()
filename = timestamp + ".log"
self.filename = filename
self.file = open(filename, mode)
self.echo = echo
if mode == "a":
self.writeln("... Continuing log at " + timestamp)
else:
self.writeln("Log opened: " + timestamp)
if name != None:
self.writeln("Experiment name: " + name)
if robot != None:
self.writeln("Robot: " + robot.type)
if brain != None:
self.writeln("Brain: " + brain.name)
if os.environ.has_key("HOSTNAME"):
self.writeln("Hostname: " + os.environ["HOSTNAME"])
if os.environ.has_key("USER"):
self.writeln("User: " + os.environ["USER"])