\var{pathname} can be either absolute (like
\file{/usr/src/Python-1.5/Makefile}) or relative (like
\file{../../Tools/*/*.gif}), and can contain shell-style wildcards.
+Broken symlinks are included in the results (as in the shell).
\end{funcdesc}
For example, consider a directory containing only the following files:
\begin{funcdesc}{exists}{path}
Return \code{True} if \var{path} refers to an existing path.
+Returns \code{False} for broken symbolic links.
+\end{funcdesc}
+
+\begin{funcdesc}{lexists}{path}
+Return \code{True} if \var{path} refers to an existing path.
+Returns \code{True} for broken symbolic links.
+Equivalent to \function{exists()} on platforms lacking
+\function{os.lstat()}.
+\versionadded{2.4}
\end{funcdesc}
\begin{funcdesc}{expanduser}{path}
"""
if not has_magic(pathname):
- if os.path.exists(pathname):
+ if os.path.lexists(pathname):
return [pathname]
else:
return []
for dirname in list:
if basename or os.path.isdir(dirname):
name = os.path.join(dirname, basename)
- if os.path.exists(name):
+ if os.path.lexists(name):
result.append(name)
else:
result = []
return os.stat(filename).st_ctime
def exists(s):
- """Return True if the pathname refers to an existing file or directory."""
+ """Test whether a path exists. Returns False for broken symbolic links"""
try:
st = os.stat(s)
return False
return True
+# Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any
+# case.
+
+def lexists(path):
+ """Test whether a path exists. Returns True for broken symbolic links"""
+
+ try:
+ st = os.lstat(path)
+ except os.error:
+ return False
+ return True
+
# Return the longest prefix of all list elements.
def commonprefix(m):
# Does a path exist?
-# This is false for dangling symbolic links.
def exists(path):
"""Test whether a path exists"""
return False
return True
+lexists = exists
+
# Is a path a dos directory?
# This follows symbolic links, so both islink() and isdir() can be true
return False
return True
+lexists = exists
+
# Is a path a directory?
except swi.error:
return 0
+lexists = exists
+
def isdir(p):
"""
return True
+# Being true for dangling symbolic links is also useful.
+
+def lexists(path):
+ """Test whether a path exists. Returns True for broken symbolic links"""
+ try:
+ st = os.lstat(path)
+ except os.error:
+ return False
+ return True
+
+
# Is a path a directory?
# This follows symbolic links, so both islink() and isdir() can be true
# for the same path.
self.mktemp('ZZZ')
self.mktemp('a', 'bcd', 'EF')
self.mktemp('a', 'bcd', 'efg', 'ha')
+ if hasattr(os, 'symlink'):
+ os.symlink(self.norm('broken'), self.norm('sym1'))
+ os.symlink(self.norm('broken'), self.norm('sym2'))
def tearDown(self):
deltree(self.tempdir)
eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'),
os.path.join('aab', 'F')]))
+ def test_glob_broken_symlinks(self):
+ if hasattr(os, 'symlink'):
+ eq = self.assertSequencesEqual_noorder
+ eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')])
+ eq(self.glob('sym1'), [self.norm('sym1')])
+ eq(self.glob('sym2'), [self.norm('sym2')])
+
def test_main():
run_unittest(GlobTests)
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()
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()
Library
-------
+- Patch #941486: added os.path.lexists(), which returns True for broken
+ symlinks, unlike os.path.exists().
+
- the random module now uses os.urandom() for seeding if it is available.
Added a new generator based on os.urandom().