Mercurial > cpython-lru_cache-weakref
annotate Lib/posixpath.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | f3cc7626a621 |
| children | 6ec6dbf787f4 |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
1 """Common operations on Posix pathnames. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
2 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
3 Instead of importing this module directly, import os and refer to |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
4 this module as os.path. The "os.path" name is an alias for this |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
5 module on Posix systems; on other systems (e.g. Mac, Windows), |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
6 os.path provides the same operations in a manner specific to that |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
7 platform, and is an alias to another module (e.g. macpath, ntpath). |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
8 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
9 Some of this can actually be useful on non-Posix systems too, e.g. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
10 for manipulation of the pathname component of URLs. |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
11 """ |
| 5 | 12 |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
13 import os |
|
64807
d390400d71f7
Added missing import.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
64798
diff
changeset
|
14 import sys |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
15 import stat |
|
41057
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
40114
diff
changeset
|
16 import genericpath |
|
46771
b93005712e39
Add Py3k warnings to os.path.walk
Benjamin Peterson <benjamin@python.org>
parents:
44522
diff
changeset
|
17 import warnings |
|
39689
d0ed7dbaad56
- Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
35377
diff
changeset
|
18 from genericpath import * |
| 5 | 19 |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
20 try: |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
21 _unicode = unicode |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
22 except NameError: |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
23 # If Python is built without Unicode support, the unicode type |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
24 # will not exist. Fake one. |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
25 class _unicode(object): |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
26 pass |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
27 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
28 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
29 "basename","dirname","commonprefix","getsize","getmtime", |
|
35377
448c6fbff641
backport bug #1266283: add lexists to os.path.__all__
Georg Brandl <georg@python.org>
parents:
35007
diff
changeset
|
30 "getatime","getctime","islink","exists","lexists","isdir","isfile", |
|
448c6fbff641
backport bug #1266283: add lexists to os.path.__all__
Georg Brandl <georg@python.org>
parents:
35007
diff
changeset
|
31 "ismount","walk","expanduser","expandvars","normpath","abspath", |
|
27056
94b9cf4274de
Fix SF #659228, 'realpath' function missing from os.path
Neal Norwitz <nnorwitz@gmail.com>
parents:
26946
diff
changeset
|
32 "samefile","sameopenfile","samestat", |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
33 "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
34 "devnull","realpath","supports_unicode_filenames","relpath"] |
| 5 | 35 |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
36 # strings representing various path-related bits and pieces |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
37 curdir = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
38 pardir = '..' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
39 extsep = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
40 sep = '/' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
41 pathsep = ':' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
42 defpath = ':/bin:/usr/bin' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
43 altsep = None |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32077
diff
changeset
|
44 devnull = '/dev/null' |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
45 |
| 582 | 46 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 47 # On MS-DOS this may also turn slashes into backslashes; however, other | |
| 48 # normalizations (such as optimizing '../' away) are not allowed | |
| 49 # (another function should be defined to do that). | |
| 50 | |
| 51 def normcase(s): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
52 """Normalize case of pathname. Has no effect under Posix""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
53 return s |
| 582 | 54 |
| 55 | |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11881
diff
changeset
|
56 # Return whether a path is absolute. |
| 582 | 57 # Trivial in Posix, harder on the Mac or MS-DOS. |
| 58 | |
| 59 def isabs(s): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
60 """Test whether a path is absolute""" |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
61 return s.startswith('/') |
| 582 | 62 |
| 63 | |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
64 # Join pathnames. |
|
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
65 # Ignore the previous parts if a part is absolute. |
| 361 | 66 # Insert a '/' unless the first part is empty or already ends in '/'. |
| 582 | 67 |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
68 def join(a, *p): |
|
42747
9a347ba17cab
Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents:
41532
diff
changeset
|
69 """Join two or more pathname components, inserting '/' as needed. |
|
9a347ba17cab
Bug #1688564: document os.path.join's absolute path behavior in the docstring.
Georg Brandl <georg@python.org>
parents:
41532
diff
changeset
|
70 If any component is an absolute path, all previous path components |
|
78225
f3cc7626a621
#15342: Add clarifying sentence to posixpath.join docstring.
R David Murray <rdmurray@bitdance.com>
parents:
77072
diff
changeset
|
71 will be discarded. An empty last part will result in a path that |
|
f3cc7626a621
#15342: Add clarifying sentence to posixpath.join docstring.
R David Murray <rdmurray@bitdance.com>
parents:
77072
diff
changeset
|
72 ends with a separator.""" |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
73 path = a |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
74 for b in p: |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
75 if b.startswith('/'): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
76 path = b |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
77 elif path == '' or path.endswith('/'): |
|
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
78 path += b |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
79 else: |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
80 path += '/' + b |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
81 return path |
| 5 | 82 |
| 83 | |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
84 # Split a path in head (everything up to the last '/') and tail (the |
|
3064
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
85 # rest). If the path ends in '/', tail will be empty. If there is no |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
86 # '/' in the path, head will be empty. |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
87 # Trailing '/'es are stripped from head unless it is the root. |
| 582 | 88 |
| 5 | 89 def split(p): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
90 """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| 14909 | 91 everything after the final slash. Either part may be empty.""" |
|
14908
8f59bd957952
Remove imports of string when string methods will do.
Fred Drake <fdrake@acm.org>
parents:
14121
diff
changeset
|
92 i = p.rfind('/') + 1 |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
93 head, tail = p[:i], p[i:] |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14909
diff
changeset
|
94 if head and head != '/'*len(head): |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
95 head = head.rstrip('/') |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
96 return head, tail |
| 5 | 97 |
| 98 | |
| 361 | 99 # Split a path in root and extension. |
|
4074
7b6eefac6e49
Correct description of splitext().
Guido van Rossum <guido@python.org>
parents:
3795
diff
changeset
|
100 # The extension is everything starting at the last dot in the last |
| 361 | 101 # pathname component; the root is everything before that. |
| 582 | 102 # It is always true that root + ext == p. |
| 103 | |
| 361 | 104 def splitext(p): |
|
41057
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
40114
diff
changeset
|
105 return genericpath._splitext(p, sep, altsep, extsep) |
|
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
40114
diff
changeset
|
106 splitext.__doc__ = genericpath._splitext.__doc__ |
| 361 | 107 |
| 2925 | 108 # Split a pathname into a drive specification and the rest of the |
| 109 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. | |
| 110 | |
| 111 def splitdrive(p): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
112 """Split a pathname into drive and path. On Posix, drive is always |
| 14909 | 113 empty.""" |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
114 return '', p |
| 2925 | 115 |
| 116 | |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
117 # Return the tail (basename) part of a path, same as split(path)[1]. |
| 582 | 118 |
| 5 | 119 def basename(p): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
120 """Returns the final component of a pathname""" |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
121 i = p.rfind('/') + 1 |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
122 return p[i:] |
| 5 | 123 |
| 124 | |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
125 # Return the head (dirname) part of a path, same as split(path)[0]. |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
126 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
127 def dirname(p): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
128 """Returns the directory component of a pathname""" |
|
40114
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
129 i = p.rfind('/') + 1 |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
130 head = p[:i] |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
131 if head and head != '/'*len(head): |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
132 head = head.rstrip('/') |
|
c4c4eec64c13
Bug #1560179: speed up posixpath.(dir|base)name
Georg Brandl <georg@python.org>
parents:
39928
diff
changeset
|
133 return head |
| 5 | 134 |
|
8746
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
6757
diff
changeset
|
135 |
| 582 | 136 # Is a path a symbolic link? |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
137 # This will always return false on systems where os.lstat doesn't exist. |
| 582 | 138 |
| 139 def islink(path): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
140 """Test whether a path is a symbolic link""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
141 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
142 st = os.lstat(path) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
143 except (os.error, AttributeError): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
144 return False |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
145 return stat.S_ISLNK(st.st_mode) |
| 582 | 146 |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
147 # Being true for dangling symbolic links is also useful. |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
148 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
149 def lexists(path): |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
150 """Test whether a path exists. Returns True for broken symbolic links""" |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
151 try: |
|
58761
cc367c077245
No need to assign the results of expressions used only for side effects.
Georg Brandl <georg@python.org>
parents:
58269
diff
changeset
|
152 os.lstat(path) |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
153 except os.error: |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
154 return False |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
155 return True |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
156 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
157 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
158 # Are two filenames really pointing to the same file? |
| 582 | 159 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
160 def samefile(f1, f2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
161 """Test whether two pathnames reference the same actual file""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
162 s1 = os.stat(f1) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
163 s2 = os.stat(f2) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
164 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
165 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
166 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
167 # Are two open files really referencing the same file? |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
168 # (Not necessarily the same file descriptor!) |
| 582 | 169 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
170 def sameopenfile(fp1, fp2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
171 """Test whether two open file objects reference the same file""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
172 s1 = os.fstat(fp1) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
173 s2 = os.fstat(fp2) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
174 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
175 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
176 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
177 # Are two stat buffers (obtained from stat, fstat or lstat) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
178 # describing the same file? |
| 582 | 179 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
180 def samestat(s1, s2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
181 """Test whether two stat buffers reference the same file""" |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
182 return s1.st_ino == s2.st_ino and \ |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
183 s1.st_dev == s2.st_dev |
| 5 | 184 |
| 185 | |
| 186 # Is a path a mount point? | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
187 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
| 582 | 188 |
| 5 | 189 def ismount(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
190 """Test whether a path is a mount point""" |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
58964
diff
changeset
|
191 if islink(path): |
|
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
58964
diff
changeset
|
192 # A symlink can never be a mount point |
|
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
58964
diff
changeset
|
193 return False |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
194 try: |
|
44468
81669fbc845f
Bug #1713: posixpath.ismount() claims symlink to a mountpoint is a mountpoint.
Christian Heimes <christian@cheimes.de>
parents:
39930
diff
changeset
|
195 s1 = os.lstat(path) |
|
81669fbc845f
Bug #1713: posixpath.ismount() claims symlink to a mountpoint is a mountpoint.
Christian Heimes <christian@cheimes.de>
parents:
39930
diff
changeset
|
196 s2 = os.lstat(join(path, '..')) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
197 except os.error: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
198 return False # It doesn't exist -- so not a mount point :-) |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
199 dev1 = s1.st_dev |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
200 dev2 = s2.st_dev |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
201 if dev1 != dev2: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
202 return True # path/.. on a different device as path |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
203 ino1 = s1.st_ino |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
204 ino2 = s2.st_ino |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
205 if ino1 == ino2: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
206 return True # path/.. is the same i-node as path |
|
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
207 return False |
| 5 | 208 |
| 209 | |
| 210 # Directory tree walk. | |
| 582 | 211 # For each directory under top (including top itself, but excluding |
| 212 # '.' and '..'), func(arg, dirname, filenames) is called, where | |
| 213 # dirname is the name of the directory and filenames is the list | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
214 # of files (and subdirectories etc.) in the directory. |
| 582 | 215 # The func may modify the filenames list, to implement a filter, |
| 5 | 216 # or to impose a different order of visiting. |
| 582 | 217 |
| 5 | 218 def walk(top, func, arg): |
|
20361
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
219 """Directory tree walk with callback function. |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
220 |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
221 For each directory in the directory tree rooted at top (including top |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
222 itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
223 dirname is the name of the directory, and fnames a list of the names of |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
224 the files and subdirectories in dirname (excluding '.' and '..'). func |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
225 may modify the fnames list in-place (e.g. via del or slice assignment), |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
226 and walk will only recurse into the subdirectories whose names remain in |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
227 fnames; this can be used to implement a filter, or to impose a specific |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
228 order of visiting. No semantics are defined for, or required of, arg, |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
229 beyond that arg is always passed to func. It can be used, e.g., to pass |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
230 a filename pattern, or a mutable object designed to accumulate |
|
c408333d37f0
SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
19953
diff
changeset
|
231 statistics. Passing None for arg is common.""" |
|
53903
a36fa2ce61b1
Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents:
52903
diff
changeset
|
232 warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.", |
|
a36fa2ce61b1
Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents:
52903
diff
changeset
|
233 stacklevel=2) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
234 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
235 names = os.listdir(top) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
236 except os.error: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
237 return |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
238 func(arg, top, names) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
239 for name in names: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
240 name = join(top, name) |
|
17549
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
241 try: |
|
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
242 st = os.lstat(name) |
|
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
243 except os.error: |
|
693fa6b41dfa
In walk(), don't die when os.lstat() raises os.error, e.g. because a
Guido van Rossum <guido@python.org>
parents:
16562
diff
changeset
|
244 continue |
|
23709
3b379ae3569d
Remove another reference to stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents:
23625
diff
changeset
|
245 if stat.S_ISDIR(st.st_mode): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
246 walk(name, func, arg) |
| 582 | 247 |
| 248 | |
| 249 # Expand paths beginning with '~' or '~user'. | |
| 250 # '~' means $HOME; '~user' means that user's home directory. | |
| 251 # If the path doesn't begin with '~', or if the user or $HOME is unknown, | |
| 252 # the path is returned unchanged (leaving error reporting to whatever | |
| 253 # function is called with the expanded path as argument). | |
| 254 # See also module 'glob' for expansion of *, ? and [...] in pathnames. | |
| 255 # (A function should also be defined to do full *sh-style environment | |
| 256 # variable expansion.) | |
| 257 | |
| 258 def expanduser(path): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
259 """Expand ~ and ~user constructions. If user or $HOME is unknown, |
| 14909 | 260 do nothing.""" |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
261 if not path.startswith('~'): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
262 return path |
|
29540
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
263 i = path.find('/', 1) |
|
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
264 if i < 0: |
|
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
265 i = len(path) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
266 if i == 1: |
|
29540
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
267 if 'HOME' not in os.environ: |
|
25126
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
268 import pwd |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
269 userhome = pwd.getpwuid(os.getuid()).pw_dir |
|
25126
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
270 else: |
|
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
271 userhome = os.environ['HOME'] |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
272 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
273 import pwd |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
274 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
275 pwent = pwd.getpwnam(path[1:i]) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
276 except KeyError: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
277 return path |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
278 userhome = pwent.pw_dir |
|
76853
299dc54ad014
Closes #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
279 userhome = userhome.rstrip('/') |
|
299dc54ad014
Closes #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
280 return (userhome + path[i:]) or '/' |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
281 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
282 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
283 # Expand paths containing shell variable substitutions. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
284 # This expands the forms $variable and ${variable} only. |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11881
diff
changeset
|
285 # Non-existent variables are left unchanged. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
286 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
287 _varprog = None |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
288 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
289 def expandvars(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
290 """Expand shell variables of form $var and ${var}. Unknown variables |
| 14909 | 291 are left unchanged.""" |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
292 global _varprog |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
293 if '$' not in path: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
294 return path |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
295 if not _varprog: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
296 import re |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
297 _varprog = re.compile(r'\$(\w+|\{[^}]*\})') |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
298 i = 0 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
299 while True: |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
300 m = _varprog.search(path, i) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
301 if not m: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
302 break |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
303 i, j = m.span(0) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
304 name = m.group(1) |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
305 if name.startswith('{') and name.endswith('}'): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
306 name = name[1:-1] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22867
diff
changeset
|
307 if name in os.environ: |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
308 tail = path[j:] |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
309 path = path[:i] + os.environ[name] |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
310 i = len(path) |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
311 path += tail |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
312 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
313 i = j |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
314 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
315 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
316 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
317 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
318 # It should be understood that this may change the meaning of the path |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
319 # if it contains symbolic links! |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
320 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
321 def normpath(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
322 """Normalize path, eliminating double slashes, etc.""" |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
323 # Preserve unicode (if path is unicode) |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
324 slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.') |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
325 if path == '': |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
326 return dot |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
327 initial_slashes = path.startswith('/') |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
328 # POSIX allows one or two initial slashes, but treats three or more |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
329 # as single slash. |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16333
diff
changeset
|
330 if (initial_slashes and |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
331 path.startswith('//') and not path.startswith('///')): |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
332 initial_slashes = 2 |
|
14908
8f59bd957952
Remove imports of string when string methods will do.
Fred Drake <fdrake@acm.org>
parents:
14121
diff
changeset
|
333 comps = path.split('/') |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
334 new_comps = [] |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
335 for comp in comps: |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
336 if comp in ('', '.'): |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
337 continue |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
338 if (comp != '..' or (not initial_slashes and not new_comps) or |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
339 (new_comps and new_comps[-1] == '..')): |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
340 new_comps.append(comp) |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
341 elif new_comps: |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
342 new_comps.pop() |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
343 comps = new_comps |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
344 path = slash.join(comps) |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
345 if initial_slashes: |
|
58270
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
346 path = slash*initial_slashes + path |
|
2f54f7e9c2b1
Merged revisions 77442 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53903
diff
changeset
|
347 return path or dot |
| 10018 | 348 |
| 349 | |
| 350 def abspath(path): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
351 """Return an absolute path.""" |
| 10018 | 352 if not isabs(path): |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
76853
diff
changeset
|
353 if isinstance(path, _unicode): |
|
58964
7ab63884d68b
Merged revisions 78247 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58270
diff
changeset
|
354 cwd = os.getcwdu() |
|
7ab63884d68b
Merged revisions 78247 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58270
diff
changeset
|
355 else: |
|
7ab63884d68b
Merged revisions 78247 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58270
diff
changeset
|
356 cwd = os.getcwd() |
|
7ab63884d68b
Merged revisions 78247 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58270
diff
changeset
|
357 path = join(cwd, path) |
| 10018 | 358 return normpath(path) |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
359 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
360 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
361 # Return a canonical path (i.e. the absolute location of a file on the |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
362 # filesystem). |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
363 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
364 def realpath(filename): |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
365 """Return the canonical path of the specified filename, eliminating any |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
366 symbolic links encountered in the path.""" |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
367 if isabs(filename): |
|
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
368 bits = ['/'] + filename.split('/')[1:] |
|
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
369 else: |
|
35007
de76b343d9c8
Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents:
33332
diff
changeset
|
370 bits = [''] + filename.split('/') |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
371 |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
372 for i in range(2, len(bits)+1): |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
373 component = join(*bits[0:i]) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
374 # Resolve symbolic links. |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
375 if islink(component): |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
376 resolved = _resolve_link(component) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
377 if resolved is None: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
378 # Infinite loop -- return original component + rest of the path |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
379 return abspath(join(*([component] + bits[i:]))) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
380 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
381 newpath = join(*([resolved] + bits[i:])) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
382 return realpath(newpath) |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19934
diff
changeset
|
383 |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
384 return abspath(filename) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
385 |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
386 |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
387 def _resolve_link(path): |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
388 """Internal helper function. Takes a path and follows symlinks |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32621
diff
changeset
|
389 until we either arrive at something that isn't a symlink, or |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
390 encounter a path we've seen before (meaning that there's a loop). |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
391 """ |
|
51099
33291e1e87ec
excellent place to use a set() #5069
Benjamin Peterson <benjamin@python.org>
parents:
46771
diff
changeset
|
392 paths_seen = set() |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
393 while islink(path): |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
394 if path in paths_seen: |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
395 # Already seen this path, so we must have a symlink loop |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
396 return None |
|
51099
33291e1e87ec
excellent place to use a set() #5069
Benjamin Peterson <benjamin@python.org>
parents:
46771
diff
changeset
|
397 paths_seen.add(path) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
398 # Resolve where the link points to |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
399 resolved = os.readlink(path) |
|
32901
30d86e356bf2
Use isabs() in conditional, not abspath
Andrew M. Kuchling <amk@amk.ca>
parents:
32736
diff
changeset
|
400 if not isabs(resolved): |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
401 dir = dirname(path) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
402 path = normpath(join(dir, resolved)) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
403 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
404 path = normpath(resolved) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
405 return path |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
406 |
|
64798
938d84257d11
Merged revisions 84866 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63437
diff
changeset
|
407 supports_unicode_filenames = (sys.platform == 'darwin') |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
408 |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
409 def relpath(path, start=curdir): |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
410 """Return a relative version of a path""" |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
411 |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
412 if not path: |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
413 raise ValueError("no path specified") |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41218
diff
changeset
|
414 |
|
65524
2f7e01694926
Issue #5117: Fixed root directory related issue on posixpath.relpath() and
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
64807
diff
changeset
|
415 start_list = [x for x in abspath(start).split(sep) if x] |
|
2f7e01694926
Issue #5117: Fixed root directory related issue on posixpath.relpath() and
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
64807
diff
changeset
|
416 path_list = [x for x in abspath(path).split(sep) if x] |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41218
diff
changeset
|
417 |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
418 # Work out how much of the filepath is shared by start and path. |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
419 i = len(commonprefix([start_list, path_list])) |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
420 |
|
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
421 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
|
44522
4321a5abd6ce
#1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents:
44467
diff
changeset
|
422 if not rel_list: |
|
4321a5abd6ce
#1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents:
44467
diff
changeset
|
423 return curdir |
|
41218
f3d4274d063d
Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
424 return join(*rel_list) |
