Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
15e22e1c
Commit
15e22e1c
authored
Dec 05, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added doc strings and reindented according to new standard, without tabs.
parent
7f973288
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
223 additions
and
189 deletions
+223
-189
Lib/ntpath.py
Lib/ntpath.py
+223
-189
No files found.
Lib/ntpath.py
View file @
15e22e1c
# Module 'ntpath' -- common operations on DOS pathnames
# Module 'ntpath' -- common operations on WinNT/Win95 pathnames
"""Common pathname manipulations, WindowsNT/95 version.
Instead of importing this module
directly, import os and refer to this module as os.path.
"""
import
os
import
os
import
stat
import
stat
...
@@ -10,9 +14,11 @@ import string
...
@@ -10,9 +14,11 @@ import string
# (this is done by normpath).
# (this is done by normpath).
_normtable
=
string
.
maketrans
(
string
.
uppercase
+
"
\
\
/"
,
_normtable
=
string
.
maketrans
(
string
.
uppercase
+
"
\
\
/"
,
string
.
lowercase
+
os
.
sep
*
2
)
string
.
lowercase
+
os
.
sep
*
2
)
def
normcase
(
s
):
def
normcase
(
s
):
"""Normalize case of pathname. Makes all characters lowercase and all
slashes into backslashes"""
return
string
.
translate
(
s
,
_normtable
)
return
string
.
translate
(
s
,
_normtable
)
...
@@ -23,31 +29,35 @@ def normcase(s):
...
@@ -23,31 +29,35 @@ def normcase(s):
# a slash or backslash.
# a slash or backslash.
def
isabs
(
s
):
def
isabs
(
s
):
s
=
splitdrive
(
s
)[
1
]
"""Test whether a path is absolute"""
return
s
!=
''
and
s
[:
1
]
in
'/
\
\
'
s
=
splitdrive
(
s
)[
1
]
return
s
!=
''
and
s
[:
1
]
in
'/
\
\
'
# Join two (or more) paths.
# Join two (or more) paths.
def
join
(
a
,
*
p
):
def
join
(
a
,
*
p
):
path
=
a
"""Join two or more pathname components, inserting "
\
\
" as needed"""
for
b
in
p
:
path
=
a
if
isabs
(
b
):
for
b
in
p
:
path
=
b
if
isabs
(
b
):
elif
path
==
''
or
path
[
-
1
:]
in
'/
\
\
'
:
path
=
b
path
=
path
+
b
elif
path
==
''
or
path
[
-
1
:]
in
'/
\
\
'
:
else
:
path
=
path
+
b
path
=
path
+
os
.
sep
+
b
else
:
return
path
path
=
path
+
os
.
sep
+
b
return
path
# Split a path in a drive specification (a drive letter followed by a
# Split a path in a drive specification (a drive letter followed by a
# colon) and the path specification.
# colon) and the path specification.
# It is always true that drivespec + pathspec == p
# It is always true that drivespec + pathspec == p
def
splitdrive
(
p
):
def
splitdrive
(
p
):
if
p
[
1
:
2
]
==
':'
:
"""Split a pathname into drive and path specifiers. Returns a 2-tuple
return
p
[
0
:
2
],
p
[
2
:]
"(drive,path)"; either part may be empty"""
return
''
,
p
if
p
[
1
:
2
]
==
':'
:
return
p
[
0
:
2
],
p
[
2
:]
return
''
,
p
# Split a path in head (everything up to the last '/') and tail (the
# Split a path in head (everything up to the last '/') and tail (the
...
@@ -57,25 +67,27 @@ def splitdrive(p):
...
@@ -57,25 +67,27 @@ def splitdrive(p):
# The resulting head won't end in '/' unless it is the root.
# The resulting head won't end in '/' unless it is the root.
def
split
(
p
):
def
split
(
p
):
d
,
p
=
splitdrive
(
p
)
"""Split a pathname. Returns tuple "(head, tail)" where "tail" is
slashes
=
''
everything after the final slash. Either part may be empty"""
while
p
and
p
[
-
1
:]
in
'/
\
\
'
:
d
,
p
=
splitdrive
(
p
)
slashes
=
slashes
+
p
[
-
1
]
slashes
=
''
p
=
p
[:
-
1
]
while
p
and
p
[
-
1
:]
in
'/
\
\
'
:
if
p
==
''
:
slashes
=
slashes
+
p
[
-
1
]
p
=
p
+
slashes
p
=
p
[:
-
1
]
head
,
tail
=
''
,
''
if
p
==
''
:
for
c
in
p
:
p
=
p
+
slashes
tail
=
tail
+
c
head
,
tail
=
''
,
''
if
c
in
'/
\
\
'
:
for
c
in
p
:
head
,
tail
=
head
+
tail
,
''
tail
=
tail
+
c
slashes
=
''
if
c
in
'/
\
\
'
:
while
head
and
head
[
-
1
:]
in
'/
\
\
'
:
head
,
tail
=
head
+
tail
,
''
slashes
=
slashes
+
head
[
-
1
]
slashes
=
''
head
=
head
[:
-
1
]
while
head
and
head
[
-
1
:]
in
'/
\
\
'
:
if
head
==
''
:
slashes
=
slashes
+
head
[
-
1
]
head
=
head
+
slashes
head
=
head
[:
-
1
]
return
d
+
head
,
tail
if
head
==
''
:
head
=
head
+
slashes
return
d
+
head
,
tail
# Split a path in root and extension.
# Split a path in root and extension.
...
@@ -84,64 +96,71 @@ def split(p):
...
@@ -84,64 +96,71 @@ def split(p):
# It is always true that root + ext == p.
# It is always true that root + ext == p.
def
splitext
(
p
):
def
splitext
(
p
):
root
,
ext
=
''
,
''
"""Split the extension from a pathname. Extension is everything from the
for
c
in
p
:
last dot to the end. Returns "(root, ext)", either part may be empty"""
if
c
in
[
'/'
,
'
\
\
'
]:
root
,
ext
=
''
,
''
root
,
ext
=
root
+
ext
+
c
,
''
for
c
in
p
:
elif
c
==
'.'
:
if
c
in
[
'/'
,
'
\
\
'
]:
if
ext
:
root
,
ext
=
root
+
ext
+
c
,
''
root
,
ext
=
root
+
ext
,
c
elif
c
==
'.'
:
else
:
if
ext
:
ext
=
c
root
,
ext
=
root
+
ext
,
c
elif
ext
:
else
:
ext
=
ext
+
c
ext
=
c
else
:
elif
ext
:
root
=
root
+
c
ext
=
ext
+
c
return
root
,
ext
else
:
root
=
root
+
c
return
root
,
ext
# Return the tail (basename) part of a path.
# Return the tail (basename) part of a path.
def
basename
(
p
):
def
basename
(
p
):
return
split
(
p
)[
1
]
"""Returns the final component of a pathname"""
return
split
(
p
)[
1
]
# Return the head (dirname) part of a path.
# Return the head (dirname) part of a path.
def
dirname
(
p
):
def
dirname
(
p
):
return
split
(
p
)[
0
]
"""Returns the directory component of a pathname"""
return
split
(
p
)[
0
]
# Return the longest prefix of all list elements.
# Return the longest prefix of all list elements.
def
commonprefix
(
m
):
def
commonprefix
(
m
):
if
not
m
:
return
''
"Given a list of pathnames, returns the longest common leading component"
prefix
=
m
[
0
]
if
not
m
:
return
''
for
item
in
m
:
prefix
=
m
[
0
]
for
i
in
range
(
len
(
prefix
)):
for
item
in
m
:
if
prefix
[:
i
+
1
]
<>
item
[:
i
+
1
]:
for
i
in
range
(
len
(
prefix
)):
prefix
=
prefix
[:
i
]
if
prefix
[:
i
+
1
]
<>
item
[:
i
+
1
]:
if
i
==
0
:
return
''
prefix
=
prefix
[:
i
]
break
if
i
==
0
:
return
''
return
prefix
break
return
prefix
# Is a path a symbolic link?
# Is a path a symbolic link?
# This will always return false on systems where posix.lstat doesn't exist.
# This will always return false on systems where posix.lstat doesn't exist.
def
islink
(
path
):
def
islink
(
path
):
return
0
"""Test for symbolic link. On WindowsNT/95 always returns false"""
return
0
# Does a path exist?
# Does a path exist?
# This is false for dangling symbolic links.
# This is false for dangling symbolic links.
def
exists
(
path
):
def
exists
(
path
):
try
:
"""Test whether a path exists"""
st
=
os
.
stat
(
path
)
try
:
except
os
.
error
:
st
=
os
.
stat
(
path
)
return
0
except
os
.
error
:
return
1
return
0
return
1
# Is a path a dos directory?
# Is a path a dos directory?
...
@@ -149,11 +168,12 @@ def exists(path):
...
@@ -149,11 +168,12 @@ def exists(path):
# for the same path.
# for the same path.
def
isdir
(
path
):
def
isdir
(
path
):
try
:
"""Test whether a path is a directory"""
st
=
os
.
stat
(
path
)
try
:
except
os
.
error
:
st
=
os
.
stat
(
path
)
return
0
except
os
.
error
:
return
stat
.
S_ISDIR
(
st
[
stat
.
ST_MODE
])
return
0
return
stat
.
S_ISDIR
(
st
[
stat
.
ST_MODE
])
# Is a path a regular file?
# Is a path a regular file?
...
@@ -161,19 +181,21 @@ def isdir(path):
...
@@ -161,19 +181,21 @@ def isdir(path):
# for the same path.
# for the same path.
def
isfile
(
path
):
def
isfile
(
path
):
try
:
"""Test whether a path is a regular file"""
st
=
os
.
stat
(
path
)
try
:
except
os
.
error
:
st
=
os
.
stat
(
path
)
return
0
except
os
.
error
:
return
stat
.
S_ISREG
(
st
[
stat
.
ST_MODE
])
return
0
return
stat
.
S_ISREG
(
st
[
stat
.
ST_MODE
])
# Are two filenames really pointing to the same file?
# Are two filenames really pointing to the same file?
def
samefile
(
f1
,
f2
):
def
samefile
(
f1
,
f2
):
s1
=
os
.
stat
(
f1
)
"""Test whether two pathnames reference the same actual file"""
s2
=
os
.
stat
(
f2
)
s1
=
os
.
stat
(
f1
)
return
samestat
(
s1
,
s2
)
s2
=
os
.
stat
(
f2
)
return
samestat
(
s1
,
s2
)
# Are two open files really referencing the same file?
# Are two open files really referencing the same file?
...
@@ -181,24 +203,28 @@ def samefile(f1, f2):
...
@@ -181,24 +203,28 @@ def samefile(f1, f2):
# XXX THIS IS BROKEN UNDER DOS! ST_INO seems to indicate number of reads?
# XXX THIS IS BROKEN UNDER DOS! ST_INO seems to indicate number of reads?
def
sameopenfile
(
fp1
,
fp2
):
def
sameopenfile
(
fp1
,
fp2
):
s1
=
os
.
fstat
(
fp1
.
fileno
())
"""Test whether two open file objects reference the same file (may not
s2
=
os
.
fstat
(
fp2
.
fileno
())
work correctly)"""
return
samestat
(
s1
,
s2
)
s1
=
os
.
fstat
(
fp1
.
fileno
())
s2
=
os
.
fstat
(
fp2
.
fileno
())
return
samestat
(
s1
,
s2
)
# Are two stat buffers (obtained from stat, fstat or lstat)
# Are two stat buffers (obtained from stat, fstat or lstat)
# describing the same file?
# describing the same file?
def
samestat
(
s1
,
s2
):
def
samestat
(
s1
,
s2
):
return
s1
[
stat
.
ST_INO
]
==
s2
[
stat
.
ST_INO
]
and
\
"""Test whether two stat buffers reference the same file"""
s1
[
stat
.
ST_DEV
]
==
s2
[
stat
.
ST_DEV
]
return
s1
[
stat
.
ST_INO
]
==
s2
[
stat
.
ST_INO
]
and
\
s1
[
stat
.
ST_DEV
]
==
s2
[
stat
.
ST_DEV
]
# Is a path a mount point?
# Is a path a mount point?
# XXX This degenerates in: 'is this the root?' on DOS
# XXX This degenerates in: 'is this the root?' on DOS
def
ismount
(
path
):
def
ismount
(
path
):
return
isabs
(
splitdrive
(
path
)[
1
])
"""Test whether a path is a mount point"""
return
isabs
(
splitdrive
(
path
)[
1
])
# Directory tree walk.
# Directory tree walk.
...
@@ -210,17 +236,21 @@ def ismount(path):
...
@@ -210,17 +236,21 @@ def ismount(path):
# or to impose a different order of visiting.
# or to impose a different order of visiting.
def
walk
(
top
,
func
,
arg
):
def
walk
(
top
,
func
,
arg
):
try
:
"""walk(top,func,args) calls func(arg, d, files) for each directory "d"
names
=
os
.
listdir
(
top
)
in the tree rooted at "top" (including "top" itself). "files" is a list
except
os
.
error
:
of all the files and subdirs in directory "d".
return
"""
func
(
arg
,
top
,
names
)
try
:
exceptions
=
(
'.'
,
'..'
)
names
=
os
.
listdir
(
top
)
for
name
in
names
:
except
os
.
error
:
if
name
not
in
exceptions
:
return
name
=
join
(
top
,
name
)
func
(
arg
,
top
,
names
)
if
isdir
(
name
):
exceptions
=
(
'.'
,
'..'
)
walk
(
name
,
func
,
arg
)
for
name
in
names
:
if
name
not
in
exceptions
:
name
=
join
(
top
,
name
)
if
isdir
(
name
):
walk
(
name
,
func
,
arg
)
# Expand paths beginning with '~' or '~user'.
# Expand paths beginning with '~' or '~user'.
...
@@ -233,86 +263,90 @@ def walk(top, func, arg):
...
@@ -233,86 +263,90 @@ def walk(top, func, arg):
# variable expansion.)
# variable expansion.)
def
expanduser
(
path
):
def
expanduser
(
path
):
if
path
[:
1
]
<>
'~'
:
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
return
path
do nothing"""
i
,
n
=
1
,
len
(
path
)
if
path
[:
1
]
<>
'~'
:
while
i
<
n
and
path
[
i
]
not
in
'/
\
\
'
:
return
path
i
=
i
+
1
i
,
n
=
1
,
len
(
path
)
if
i
==
1
:
while
i
<
n
and
path
[
i
]
not
in
'/
\
\
'
:
if
os
.
environ
.
has_key
(
'HOME'
):
i
=
i
+
1
userhome
=
os
.
environ
[
'HOME'
]
if
i
==
1
:
elif
not
os
.
environ
.
has_key
(
'HOMEPATH'
):
if
os
.
environ
.
has_key
(
'HOME'
):
return
path
userhome
=
os
.
environ
[
'HOME'
]
else
:
elif
not
os
.
environ
.
has_key
(
'HOMEPATH'
):
try
:
return
path
drive
=
os
.
environ
[
'HOMEDRIVE'
]
else
:
except
KeyError
:
try
:
drive
=
''
drive
=
os
.
environ
[
'HOMEDRIVE'
]
userhome
=
join
(
drive
,
os
.
environ
[
'HOMEPATH'
])
except
KeyError
:
else
:
drive
=
''
return
path
userhome
=
join
(
drive
,
os
.
environ
[
'HOMEPATH'
])
return
userhome
+
path
[
i
:]
else
:
return
path
return
userhome
+
path
[
i
:]
# Expand paths containing shell variable substitutions.
# Expand paths containing shell variable substitutions.
# The following rules apply:
# The following rules apply:
#
- no expansion within single quotes
#
- no expansion within single quotes
#
- no escape character, except for '$$' which is translated into '$'
#
- no escape character, except for '$$' which is translated into '$'
#
- ${varname} is accepted.
#
- ${varname} is accepted.
#
- varnames can be made out of letters, digits and the character '_'
#
- varnames can be made out of letters, digits and the character '_'
# XXX With COMMAND.COM you can use any characters in a variable name,
# XXX With COMMAND.COM you can use any characters in a variable name,
# XXX except '^|<>='.
# XXX except '^|<>='.
varchars
=
string
.
letters
+
string
.
digits
+
'_-'
varchars
=
string
.
letters
+
string
.
digits
+
'_-'
def
expandvars
(
path
):
def
expandvars
(
path
):
if
'$'
not
in
path
:
"""Expand shell variables of form $var and ${var}. Unknown variables
return
path
are left unchanged"""
res
=
''
if
'$'
not
in
path
:
index
=
0
return
path
pathlen
=
len
(
path
)
res
=
''
while
index
<
pathlen
:
index
=
0
c
=
path
[
index
]
pathlen
=
len
(
path
)
if
c
==
'
\
'
'
:
# no expansion within single quotes
while
index
<
pathlen
:
path
=
path
[
index
+
1
:]
c
=
path
[
index
]
pathlen
=
len
(
path
)
if
c
==
'
\
'
'
:
# no expansion within single quotes
try
:
path
=
path
[
index
+
1
:]
index
=
string
.
index
(
path
,
'
\
'
'
)
pathlen
=
len
(
path
)
res
=
res
+
'
\
'
'
+
path
[:
index
+
1
]
try
:
except
string
.
index_error
:
index
=
string
.
index
(
path
,
'
\
'
'
)
res
=
res
+
path
res
=
res
+
'
\
'
'
+
path
[:
index
+
1
]
index
=
pathlen
-
1
except
string
.
index_error
:
elif
c
==
'$'
:
# variable or '$$'
res
=
res
+
path
if
path
[
index
+
1
:
index
+
2
]
==
'$'
:
index
=
pathlen
-
1
res
=
res
+
c
elif
c
==
'$'
:
# variable or '$$'
index
=
index
+
1
if
path
[
index
+
1
:
index
+
2
]
==
'$'
:
elif
path
[
index
+
1
:
index
+
2
]
==
'{'
:
res
=
res
+
c
path
=
path
[
index
+
2
:]
index
=
index
+
1
pathlen
=
len
(
path
)
elif
path
[
index
+
1
:
index
+
2
]
==
'{'
:
try
:
path
=
path
[
index
+
2
:]
index
=
string
.
index
(
path
,
'}'
)
pathlen
=
len
(
path
)
var
=
path
[:
index
]
try
:
if
os
.
environ
.
has_key
(
var
):
index
=
string
.
index
(
path
,
'}'
)
res
=
res
+
os
.
environ
[
var
]
var
=
path
[:
index
]
except
string
.
index_error
:
if
os
.
environ
.
has_key
(
var
):
res
=
res
+
path
res
=
res
+
os
.
environ
[
var
]
index
=
pathlen
-
1
except
string
.
index_error
:
else
:
res
=
res
+
path
var
=
''
index
=
pathlen
-
1
index
=
index
+
1
else
:
c
=
path
[
index
:
index
+
1
]
var
=
''
while
c
!=
''
and
c
in
varchars
:
index
=
index
+
1
var
=
var
+
c
c
=
path
[
index
:
index
+
1
]
index
=
index
+
1
while
c
!=
''
and
c
in
varchars
:
c
=
path
[
index
:
index
+
1
]
var
=
var
+
c
if
os
.
environ
.
has_key
(
var
):
index
=
index
+
1
res
=
res
+
os
.
environ
[
var
]
c
=
path
[
index
:
index
+
1
]
if
c
!=
''
:
if
os
.
environ
.
has_key
(
var
):
res
=
res
+
c
res
=
res
+
os
.
environ
[
var
]
else
:
if
c
!=
''
:
res
=
res
+
c
res
=
res
+
c
index
=
index
+
1
else
:
return
res
res
=
res
+
c
index
=
index
+
1
return
res
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
...
@@ -320,25 +354,25 @@ def expandvars(path):
...
@@ -320,25 +354,25 @@ def expandvars(path):
# but as this module is called "ntpath", that's obviously wrong!
# but as this module is called "ntpath", that's obviously wrong!
def
normpath
(
path
):
def
normpath
(
path
):
path
=
normcase
(
path
)
"""Normalize path, eliminating double slashes, etc."""
prefix
,
path
=
splitdriv
e
(
path
)
path
=
normcas
e
(
path
)
while
path
[:
1
]
==
os
.
sep
:
prefix
,
path
=
splitdrive
(
path
)
prefix
=
prefix
+
os
.
sep
while
path
[:
1
]
==
os
.
sep
:
path
=
path
[
1
:]
prefix
=
prefix
+
os
.
sep
comps
=
string
.
splitfields
(
path
,
os
.
sep
)
path
=
path
[
1
:]
i
=
0
comps
=
string
.
splitfields
(
path
,
os
.
sep
)
while
i
<
len
(
comps
):
i
=
0
if
comps
[
i
]
==
'.'
:
while
i
<
len
(
comps
)
:
del
comps
[
i
]
if
comps
[
i
]
==
'.'
:
elif
comps
[
i
]
==
'..'
and
i
>
0
and
\
del
comps
[
i
]
comps
[
i
-
1
]
not
in
(
''
,
'..'
):
elif
comps
[
i
]
==
'..'
and
i
>
0
and
comps
[
i
-
1
]
not
in
(
''
,
'..'
):
del
comps
[
i
-
1
:
i
+
1
]
del
comps
[
i
-
1
:
i
+
1
]
i
=
i
-
1
i
=
i
-
1
elif
comps
[
i
]
==
''
and
i
>
0
and
comps
[
i
-
1
]
<>
''
:
elif
comps
[
i
]
==
''
and
i
>
0
and
comps
[
i
-
1
]
<>
''
:
del
comps
[
i
]
del
comps
[
i
]
else
:
else
:
i
=
i
+
1
i
=
i
+
1
# If the path is now empty, substitute '.'
# If the path is now empty, substitute '.'
if
not
prefix
and
not
comps
:
if
not
prefix
and
not
comps
:
comps
.
append
(
'.'
)
comps
.
append
(
'.'
)
return
prefix
+
string
.
joinfields
(
comps
,
os
.
sep
)
return
prefix
+
string
.
joinfields
(
comps
,
os
.
sep
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment