annotate Lib/genericpath.py @ 81343:3a7cd8efc19f

#16852: merge with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Thu, 10 Jan 2013 03:29:45 +0200
parents 189b21f9bc0c
children a9ee869cae40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
1 """
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
2 Path operations common to more than one OS
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
3 Do not use directly. The OS specific modules import the appropriate
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
4 functions from this module themselves.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
5 """
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
6 import os
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
7 import stat
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
8
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
9 __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
81062
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
10 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
11 'samestat']
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
12
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
13
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
14 # Does a path exist?
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
15 # This is false for dangling symbolic links on systems that support them.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
16 def exists(path):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
17 """Test whether a path exists. Returns False for broken symbolic links"""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
18 try:
59613
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49380
diff changeset
19 os.stat(path)
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 59613
diff changeset
20 except OSError:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
21 return False
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22 return True
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
23
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
24
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
25 # This follows symbolic links, so both islink() and isdir() can be true
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
26 # for the same path ono systems that support symlinks
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
27 def isfile(path):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
28 """Test whether a path is a regular file"""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
29 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
30 st = os.stat(path)
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 59613
diff changeset
31 except OSError:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
32 return False
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
33 return stat.S_ISREG(st.st_mode)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
34
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
35
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
36 # Is a path a directory?
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
37 # This follows symbolic links, so both islink() and isdir()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
38 # can be true for the same path on systems that support symlinks
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
39 def isdir(s):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
40 """Return true if the pathname refers to an existing directory."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
41 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
42 st = os.stat(s)
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 59613
diff changeset
43 except OSError:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
44 return False
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
45 return stat.S_ISDIR(st.st_mode)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
46
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
47
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
48 def getsize(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
49 """Return the size of a file, reported by os.stat()."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
50 return os.stat(filename).st_size
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
51
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
52
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
53 def getmtime(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
54 """Return the last modification time of a file, reported by os.stat()."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
55 return os.stat(filename).st_mtime
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
56
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
57
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
58 def getatime(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
59 """Return the last access time of a file, reported by os.stat()."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
60 return os.stat(filename).st_atime
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
61
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
62
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
63 def getctime(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
64 """Return the metadata change time of a file, reported by os.stat()."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65 return os.stat(filename).st_ctime
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
66
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
67
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
68 # Return the longest prefix of all list elements.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
69 def commonprefix(m):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
70 "Given a list of pathnames, returns the longest common leading component"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
71 if not m: return ''
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
72 s1 = min(m)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
73 s2 = max(m)
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
74 for i, c in enumerate(s1):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
75 if c != s2[i]:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
76 return s1[:i]
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
77 return s1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
78
81062
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
79 # Are two stat buffers (obtained from stat, fstat or lstat)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
80 # describing the same file?
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
81 def samestat(s1, s2):
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
82 """Test whether two stat buffers reference the same file"""
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
83 return (s1.st_ino == s2.st_ino and
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
84 s1.st_dev == s2.st_dev)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
85
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
86
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
87 # Are two filenames really pointing to the same file?
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
88 def samefile(f1, f2):
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
89 """Test whether two pathnames reference the same actual file"""
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
90 s1 = os.stat(f1)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
91 s2 = os.stat(f2)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
92 return samestat(s1, s2)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
93
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
94
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
95 # Are two open files really referencing the same file?
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
96 # (Not necessarily the same file descriptor!)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
97 def sameopenfile(fp1, fp2):
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
98 """Test whether two open file objects reference the same file"""
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
99 s1 = os.fstat(fp1)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
100 s2 = os.fstat(fp2)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
101 return samestat(s1, s2)
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
102
189b21f9bc0c Fix #11939. Set st_dev attribute on Windows to simplify os.path.samefile.
Brian Curtin <brian@python.org>
parents: 80932
diff changeset
103
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
104 # Split a path in root and extension.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
105 # The extension is everything starting at the last dot in the last
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
106 # pathname component; the root is everything before that.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
107 # It is always true that root + ext == p.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
108
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
109 # Generic implementation of splitext, to be parametrized with
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
110 # the separators
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
111 def _splitext(p, sep, altsep, extsep):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
112 """Split the extension from a pathname.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
113
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
114 Extension is everything from the last dot to the end, ignoring
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
115 leading dots. Returns "(root, ext)"; ext may be empty."""
49380
8a7c930abab6 Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
116 # NOTE: This code must work for text and bytes strings.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
117
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
118 sepIndex = p.rfind(sep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
119 if altsep:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
120 altsepIndex = p.rfind(altsep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
121 sepIndex = max(sepIndex, altsepIndex)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
122
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
123 dotIndex = p.rfind(extsep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
124 if dotIndex > sepIndex:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
125 # skip all leading dots
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
126 filenameIndex = sepIndex + 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
127 while filenameIndex < dotIndex:
49380
8a7c930abab6 Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
128 if p[filenameIndex:filenameIndex+1] != extsep:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
129 return p[:dotIndex], p[dotIndex:]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
130 filenameIndex += 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
131
49380
8a7c930abab6 Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
132 return p, p[:0]