annotate Lib/ntpath.py @ 28646:193b8aec00ce legacy-trunk

Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to work. This includes some more code that used to be part of pgen in the main parser; I'm okay with that. I'll see if the Windows build needs work next.
author Guido van Rossum <guido@python.org>
date Thu, 17 Apr 2003 14:55:42 +0000
parents 9d3a25e99075
children 6f15e73b59f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
2 """Common pathname manipulations, WindowsNT/95 version.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
3
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
4 Instead of importing this module directly, import os and refer to this
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
5 module as os.path.
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
6 """
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import os
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import stat
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
10 import sys
13581
e871bd05c6a7 forgot to change copy.copy(m) to m[:]
Skip Montanaro <skip@pobox.com>
parents: 13577
diff changeset
11
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
12 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
13 "basename","dirname","commonprefix","getsize","getmtime",
26946
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
14 "getatime","getctime", "islink","exists","isdir","isfile","ismount",
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
15 "walk","expanduser","expandvars","normpath","abspath","splitunc",
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
16 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
27056
94b9cf4274de Fix SF #659228, 'realpath' function missing from os.path
Neal Norwitz <nnorwitz@gmail.com>
parents: 26946
diff changeset
17 "realpath","supports_unicode_filenames"]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
19 # 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
20 curdir = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
21 pardir = '..'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
22 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
23 sep = '\\'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
24 pathsep = ';'
28427
9d3a25e99075 make nt altsep forward slash - closes bug 709428
Skip Montanaro <skip@pobox.com>
parents: 27908
diff changeset
25 altsep = '/'
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
26 defpath = '.;C:\\bin'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
27 if 'ce' in sys.builtin_module_names:
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
28 defpath = '\\Windows'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
29 elif 'os2' in sys.builtin_module_names:
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
30 # OS/2 w/ VACPP
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
31 altsep = '/'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
32
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
33 # Normalize the case of a pathname and map slashes to backslashes.
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
34 # Other normalizations (such as optimizing '../' away) are not done
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
36
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
37 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
38 """Normalize case of pathname.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
40 Makes all characters lowercase and all slashes into backslashes."""
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
41 return s.replace("/", "\\").lower()
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
43
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
44 # Return whether a path is absolute.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # Trivial in Posix, harder on the Mac or MS-DOS.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # For DOS it is absolute if it starts with a slash or backslash (current
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
47 # volume), or if a pathname after the volume letter and colon / UNC resource
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
48 # starts with a slash or backslash.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def isabs(s):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
51 """Test whether a path is absolute"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
52 s = splitdrive(s)[1]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
53 return s != '' and s[:1] in '/\\'
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
56 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
57
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
58 def join(a, *p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
59 """Join two or more pathname components, inserting "\\" as needed"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
60 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
61 for b in p:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
62 b_wins = 0 # set to 1 iff b makes path irrelevant
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
63 if path == "":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
64 b_wins = 1
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
65
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
66 elif isabs(b):
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
67 # This probably wipes out path so far. However, it's more
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
68 # complicated if path begins with a drive letter:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
69 # 1. join('c:', '/a') == 'c:/a'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
70 # 2. join('c:/', '/a') == 'c:/a'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
71 # But
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
72 # 3. join('c:/a', '/b') == '/b'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
73 # 4. join('c:', 'd:/') = 'd:/'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
74 # 5. join('c:/', 'd:/') = 'd:/'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
75 if path[1:2] != ":" or b[1:2] == ":":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
76 # Path doesn't start with a drive letter, or cases 4 and 5.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
77 b_wins = 1
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
78
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
79 # Else path has a drive letter, and b doesn't but is absolute.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
80 elif len(path) > 3 or (len(path) == 3 and
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
81 path[-1] not in "/\\"):
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
82 # case 3
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
83 b_wins = 1
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
84
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
85 if b_wins:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
86 path = b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
87 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
88 # Join, and ensure there's a separator.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
89 assert len(path) > 0
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
90 if path[-1] in "/\\":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
91 if b and b[0] in "/\\":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
92 path += b[1:]
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
93 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
94 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
95 elif path[-1] == ":":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
96 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
97 elif b:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
98 if b[0] in "/\\":
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
99 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
100 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
101 path += "\\" + b
20887
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
102 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
103 # path is not empty and does not end with a backslash,
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
104 # but b is empty; since, e.g., split('a/') produces
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
105 # ('a', ''), it's best if join() adds a backslash in
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
106 # this case.
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
107 path += '\\'
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
108
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
109 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # Split a path in a drive specification (a drive letter followed by a
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
113 # colon) and the path specification.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 # It is always true that drivespec + pathspec == p
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 def splitdrive(p):
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
116 """Split a pathname into drive and path specifiers. Returns a 2-tuple
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
117 "(drive,path)"; either part may be empty"""
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
118 if p[1:2] == ':':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
119 return p[0:2], p[2:]
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
120 return '', p
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
121
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
122
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
123 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
124 def splitunc(p):
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
125 """Split a pathname into UNC mount point and relative path specifiers.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
126
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
127 Return a 2-tuple (unc, rest); either part may be empty.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
128 If unc is not empty, it has the form '//host/mount' (or similar
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
129 using backslashes). unc+rest is always the input path.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
130 Paths containing drive letters never have an UNC part.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
131 """
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
132 if p[1:2] == ':':
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
133 return '', p # Drive letter present
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
134 firstTwo = p[0:2]
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
135 if firstTwo == '//' or firstTwo == '\\\\':
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
136 # is a UNC path:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
137 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
138 # \\machine\mountpoint\directories...
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
139 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
140 normp = normcase(p)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
141 index = normp.find('\\', 2)
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
142 if index == -1:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
143 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
144 return ("", p)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
145 index = normp.find('\\', index + 1)
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
146 if index == -1:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
147 index = len(p)
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
148 return p[:index], p[index:]
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
149 return '', p
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 # Split a path in head (everything up to the last '/') and tail (the
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
153 # rest). After the trailing '/' is stripped, the invariant
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 # join(head, tail) == p holds.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 # The resulting head won't end in '/' unless it is the root.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 def split(p):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
158 """Split a pathname.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
159
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
160 Return tuple (head, tail) where tail is everything after the final slash.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
161 Either part may be empty."""
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
162
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
163 d, p = splitdrive(p)
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
164 # set i to index beyond p's last slash
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
165 i = len(p)
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
166 while i and p[i-1] not in '/\\':
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
167 i = i - 1
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
168 head, tail = p[:i], p[i:] # now tail has no slashes
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
169 # remove trailing slashes from head, unless it's all slashes
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
170 head2 = head
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
171 while head2 and head2[-1] in '/\\':
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
172 head2 = head2[:-1]
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
173 head = head2 or head
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
174 return d + head, tail
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 # Split a path in root and extension.
5027
0ea4ea5b2eb7 Fix splitext() to go up to the last dot, not the first.
Guido van Rossum <guido@python.org>
parents: 4150
diff changeset
178 # The extension is everything starting at the last dot in the last
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 # pathname component; the root is everything before that.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 # It is always true that root + ext == p.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 def splitext(p):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
183 """Split the extension from a pathname.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
184
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
185 Extension is everything from the last dot to the end.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
186 Return (root, ext), either part may be empty."""
26627
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
187
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
188 i = p.rfind('.')
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
189 if i<=max(p.rfind('/'), p.rfind('\\')):
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
190 return p, ''
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
191 else:
7babdaea7d19 Patch #536661: Improve performance of splitext. Add test_macpath.
Martin v. Löwis <martin@v.loewis.de>
parents: 25676
diff changeset
192 return p[:i], p[i:]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 # Return the tail (basename) part of a path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 def basename(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
198 """Returns the final component of a pathname"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
199 return split(p)[1]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 # Return the head (dirname) part of a path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 def dirname(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
205 """Returns the directory component of a pathname"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
206 return split(p)[0]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 # Return the longest prefix of all list elements.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 def commonprefix(m):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
212 "Given a list of pathnames, returns the longest common leading component"
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
213 if not m: return ''
14121
73c564e4a0f3 revert semantics of commonprefix to work character-by-character
Skip Montanaro <skip@pobox.com>
parents: 13964
diff changeset
214 prefix = m[0]
73c564e4a0f3 revert semantics of commonprefix to work character-by-character
Skip Montanaro <skip@pobox.com>
parents: 13964
diff changeset
215 for item in m:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
216 for i in range(len(prefix)):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
217 if prefix[:i+1] != item[:i+1]:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
218 prefix = prefix[:i]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
219 if i == 0: return ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
220 break
14121
73c564e4a0f3 revert semantics of commonprefix to work character-by-character
Skip Montanaro <skip@pobox.com>
parents: 13964
diff changeset
221 return prefix
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
224 # Get size, mtime, atime of files.
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
225
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
226 def getsize(filename):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
227 """Return the size of a file, reported by os.stat()"""
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
228 return os.stat(filename).st_size
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
229
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
230 def getmtime(filename):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
231 """Return the last modification time of a file, reported by os.stat()"""
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
232 return os.stat(filename).st_mtime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
233
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
234 def getatime(filename):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
235 """Return the last access time of a file, reported by os.stat()"""
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
236 return os.stat(filename).st_atime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
237
26946
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
238 def getctime(filename):
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
239 """Return the creation time of a file, reported by os.stat()."""
acdb39b714b1 Patch #658927: Add getctime to os.path.
Martin v. Löwis <martin@v.loewis.de>
parents: 26627
diff changeset
240 return os.stat(filename).st_ctime
8746
ca1b906149e6 Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents: 8212
diff changeset
241
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 # Is a path a symbolic link?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 # This will always return false on systems where posix.lstat doesn't exist.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def islink(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
246 """Test for symbolic link. On WindowsNT/95 always returns false"""
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
247 return False
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 # Does a path exist?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 # This is false for dangling symbolic links.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
252
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 def exists(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
254 """Test whether a path exists"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
255 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
256 st = os.stat(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
257 except os.error:
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 21743
diff changeset
258 return False
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 21743
diff changeset
259 return True
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 # Is a path a dos directory?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 # This follows symbolic links, so both islink() and isdir() can be true
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 # for the same path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 def isdir(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
267 """Test whether a path is a directory"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
268 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
269 st = os.stat(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
270 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
271 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
272 return stat.S_ISDIR(st.st_mode)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 # Is a path a regular file?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 # This follows symbolic links, so both islink() and isdir() can be true
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 # for the same path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 def isfile(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
280 """Test whether a path is a regular file"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
281 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
282 st = os.stat(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
283 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
284 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
285 return stat.S_ISREG(st.st_mode)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
288 # Is a path a mount point? Either a root (with or without drive letter)
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
289 # or an UNC path with at most a / or \ after the mount point.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 def ismount(path):
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
292 """Test whether a path is a mount point (defined as root of drive)"""
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
293 unc, rest = splitunc(path)
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
294 if unc:
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
295 return rest in ("", "/", "\\")
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
296 p = splitdrive(path)[1]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
297 return len(p) == 1 and p[0] in '/\\'
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 # Directory tree walk.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 # For each directory under top (including top itself, but excluding
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 # '.' and '..'), func(arg, dirname, filenames) is called, where
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 # dirname is the name of the directory and filenames is the list
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 # files files (and subdirectories etc.) in the directory.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 # The func may modify the filenames list, to implement a filter,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 # or to impose a different order of visiting.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 def walk(top, func, arg):
20361
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
309 """Directory tree walk with callback function.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
310
20361
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
311 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: 19934
diff changeset
312 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: 19934
diff changeset
313 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: 19934
diff changeset
314 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: 19934
diff changeset
315 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: 19934
diff changeset
316 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: 19934
diff changeset
317 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: 19934
diff changeset
318 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: 19934
diff changeset
319 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: 19934
diff changeset
320 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: 19934
diff changeset
321 statistics. Passing None for arg is common."""
c408333d37f0 SF bug [#469732] os.path.walk docstring inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 19934
diff changeset
322
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
323 try:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
324 names = os.listdir(top)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
325 except os.error:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
326 return
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
327 func(arg, top, names)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
328 exceptions = ('.', '..')
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
329 for name in names:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
330 if name not in exceptions:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
331 name = join(top, name)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
332 if isdir(name):
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
333 walk(name, func, arg)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 # Expand paths beginning with '~' or '~user'.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 # '~' means $HOME; '~user' means that user's home directory.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 # the path is returned unchanged (leaving error reporting to whatever
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 # function is called with the expanded path as argument).
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 # (A function should also be defined to do full *sh-style environment
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 def expanduser(path):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
346 """Expand ~ and ~user constructs.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
347
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
348 If user or $HOME is unknown, do nothing."""
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14910
diff changeset
349 if path[:1] != '~':
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
350 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
351 i, n = 1, len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
352 while i < n and path[i] not in '/\\':
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
353 i = i + 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
354 if i == 1:
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
355 if 'HOME' in os.environ:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
356 userhome = os.environ['HOME']
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
357 elif not 'HOMEPATH' in os.environ:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
358 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
359 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
360 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
361 drive = os.environ['HOMEDRIVE']
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
362 except KeyError:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
363 drive = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
364 userhome = join(drive, os.environ['HOMEPATH'])
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
365 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
366 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
367 return userhome + path[i:]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 # Expand paths containing shell variable substitutions.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 # The following rules apply:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
372 # - no expansion within single quotes
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
373 # - no escape character, except for '$$' which is translated into '$'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
374 # - ${varname} is accepted.
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
375 # - varnames can be made out of letters, digits and the character '_'
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 # XXX With COMMAND.COM you can use any characters in a variable name,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
378
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
379 def expandvars(path):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
380 """Expand shell variables of form $var and ${var}.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
381
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
382 Unknown variables are left unchanged."""
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
383 if '$' not in path:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
384 return path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
385 import string
18781
01ae962df885 Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents: 18742
diff changeset
386 varchars = string.ascii_letters + string.digits + '_-'
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
387 res = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
388 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
389 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
390 while index < pathlen:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
391 c = path[index]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
392 if c == '\'': # no expansion within single quotes
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
393 path = path[index + 1:]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
394 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
395 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
396 index = path.index('\'')
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
397 res = res + '\'' + path[:index + 1]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
398 except ValueError:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
399 res = res + path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
400 index = pathlen - 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
401 elif c == '$': # variable or '$$'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
402 if path[index + 1:index + 2] == '$':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
403 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
404 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
405 elif path[index + 1:index + 2] == '{':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
406 path = path[index+2:]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
407 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
408 try:
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
409 index = path.index('}')
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
410 var = path[:index]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
411 if var in os.environ:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
412 res = res + os.environ[var]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
413 except ValueError:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
414 res = res + path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
415 index = pathlen - 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
416 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
417 var = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
418 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
419 c = path[index:index + 1]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
420 while c != '' and c in varchars:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
421 var = var + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
422 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
423 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
424 if var in os.environ:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
425 res = res + os.environ[var]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
426 if c != '':
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
427 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
428 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
429 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
430 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
431 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
434 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
4150
8a0d43b13125 Don't do truncation to 8+3 format -- this is used on NT file systems!
Guido van Rossum <guido@python.org>
parents: 3953
diff changeset
435 # Previously, this function also truncated pathnames to 8+3 format,
8a0d43b13125 Don't do truncation to 8+3 format -- this is used on NT file systems!
Guido van Rossum <guido@python.org>
parents: 3953
diff changeset
436 # but as this module is called "ntpath", that's obviously wrong!
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
437
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 def normpath(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
439 """Normalize path, eliminating double slashes, etc."""
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
440 path = path.replace("/", "\\")
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
441 prefix, path = splitdrive(path)
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
442 while path[:1] == "\\":
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
443 prefix = prefix + "\\"
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
444 path = path[1:]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
445 comps = path.split("\\")
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
446 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
447 while i < len(comps):
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
448 if comps[i] in ('.', ''):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
449 del comps[i]
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
450 elif comps[i] == '..':
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
451 if i > 0 and comps[i-1] != '..':
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
452 del comps[i-1:i+1]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
453 i -= 1
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
454 elif i == 0 and prefix.endswith("\\"):
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
455 del comps[i]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
456 else:
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
457 i += 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
458 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
459 i += 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
460 # If the path is now empty, substitute '.'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
461 if not prefix and not comps:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
462 comps.append('.')
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
463 return prefix + "\\".join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
464
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
465
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
466 # Return an absolute path.
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
467 def abspath(path):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
468 """Return the absolute version of a path"""
21743
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
469 try:
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
470 from nt import _getfullpathname
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
471 except ImportError: # Not running on Windows - mock up something sensible.
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
472 global abspath
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
473 def _abspath(path):
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
474 if not isabs(path):
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
475 path = join(os.getcwd(), path)
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
476 return normpath(path)
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
477 abspath = _abspath
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
478 return _abspath(path)
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
479
13964
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
480 if path: # Empty path must return current working directory.
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
481 try:
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16460
diff changeset
482 path = _getfullpathname(path)
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16460
diff changeset
483 except WindowsError:
17836
ad500b52673c abspath(): Fix inconsistent indentation.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
484 pass # Bad path - return unchanged.
13964
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
485 else:
a256c101bf27 Fix for Bug #110673: os.abspatth() now always returns os.getcwd() on Windows, if an empty path is specified. It previously did not if an empty path was delegated to win32api.GetFullPathName())
Mark Hammond <mhammond@skippinet.com.au>
parents: 13632
diff changeset
486 path = os.getcwd()
11498
756351707cac In abspath(), always use normpath(), even when win32api is available
Guido van Rossum <guido@python.org>
parents: 11428
diff changeset
487 return normpath(path)
19934
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
488
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
489 # realpath is a no-op on systems without islink support
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
490 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
491 # Win9x family and earlier have no Unicode filename support.
25676
bac5a4932905 Don't try to access sys.getwindowsversion unless it exists (ntpath is
Tim Peters <tim.peters@gmail.com>
parents: 25666
diff changeset
492 supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
bac5a4932905 Don't try to access sys.getwindowsversion unless it exists (ntpath is
Tim Peters <tim.peters@gmail.com>
parents: 25666
diff changeset
493 sys.getwindowsversion()[3] >= 2)