annotate Lib/genericpath.py @ 74006:88198b93ff2f

Issue #13560: Add PyUnicode_EncodeLocale() * Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not available * Document my last changes in Misc/NEWS
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 17 Dec 2011 04:13:41 +0100
parents 77c71f258df7
children e07f104133d5 47f98a550d42
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',
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
10 'getsize', 'isdir', 'isfile']
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
11
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 # Does a path exist?
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
14 # 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
15 def exists(path):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
16 """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
17 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
18 os.stat(path)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
19 except os.error:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
20 return False
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
21 return True
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22
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 # 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
25 # 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
26 def isfile(path):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
27 """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
28 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
29 st = os.stat(path)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
30 except os.error:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
31 return False
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
32 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
33
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 # Is a path a directory?
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
36 # 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
37 # 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
38 def isdir(s):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
39 """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
40 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
41 st = os.stat(s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
42 except os.error:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
43 return False
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
44 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
45
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 def getsize(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
48 """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
49 return os.stat(filename).st_size
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
50
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 def getmtime(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
53 """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
54 return os.stat(filename).st_mtime
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
55
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 def getatime(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
58 """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
59 return os.stat(filename).st_atime
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
60
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 def getctime(filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
63 """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
64 return os.stat(filename).st_ctime
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65
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 # 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
68 def commonprefix(m):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
69 "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
70 if not m: return ''
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
71 s1 = min(m)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
72 s2 = max(m)
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
73 for i, c in enumerate(s1):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
74 if c != s2[i]:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
diff changeset
75 return s1[:i]
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
76 return s1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
77
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
78 # 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
79 # 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
80 # 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
81 # 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
82
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
83 # 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
84 # the separators
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
85 def _splitext(p, sep, altsep, extsep):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
86 """Split the extension from a pathname.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
87
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
88 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
89 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
90 # 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
91
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
92 sepIndex = p.rfind(sep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
93 if altsep:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
94 altsepIndex = p.rfind(altsep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
95 sepIndex = max(sepIndex, altsepIndex)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
96
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
97 dotIndex = p.rfind(extsep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
98 if dotIndex > sepIndex:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
99 # skip all leading dots
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
100 filenameIndex = sepIndex + 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
101 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
102 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
103 return p[:dotIndex], p[dotIndex:]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
104 filenameIndex += 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
105
49380
8a7c930abab6 Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
106 return p, p[:0]