Python posixpath 模块,exists() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用posixpath.exists()。
def makedirs(name, mode=0777):
"""makedirs(path [,mode=0777])
Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir,except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive.
"""
head, tail = path.split(name)
if not tail:
head, tail = path.split(head)
if head and tail and not path.exists(head):
try:
makedirs(head, mode)
except OSError, e:
# be happy if someone already created the path
if e.errno != errno.EEXIST:
raise
if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
return
mkdir(name, mode)
def renames(old, new):
"""renames(old,new)
Super-rename; create directories as necessary and delete any left
empty. Works like rename,except creation of any intermediate
directories needed to make the new pathname good is attempted
first. After the rename,directories corresponding to rightmost
path segments of the old name will be pruned until either the
whole path is consumed or a nonempty directory is found.
Note: this function can fail with the new directory structure made
if you lack permissions needed to unlink the leaf directory or
file.
"""
head, tail = path.split(new)
if head and tail and not path.exists(head):
makedirs(head)
rename(old, new)
head, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except OSError:
pass
def renames(old,directories corresponding to rightmost
path segments of the old name will be pruned way until either the
whole path is consumed or a nonempty directory is found.
Note: this function can fail with the new directory structure made
if you lack permissions needed to unlink the leaf directory or
file.
"""
head, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except OSError:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def test_islink(self):
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
self.assertIs(posixpath.lexists(support.TESTFN + "2"), False)
f = open(support.TESTFN + "1", "wb")
try:
f.write(b"foo")
f.close()
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
if support.can_symlink():
os.symlink(support.TESTFN + "1", support.TESTFN + "2")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
os.remove(support.TESTFN + "1")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
self.assertIs(posixpath.exists(support.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(support.TESTFN + "2"), True)
finally:
if not f.close():
f.close()
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except OSError:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except OSError:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
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()
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except error:
pass
def test_islink(self):
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), True)
finally:
if not f.close():
f.close()
def renames(old, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except OSError:
pass
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。