Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
ntpath Namespace Reference

Functions

def normcase
 
def isabs
 
def join
 
def splitdrive
 
def splitunc
 
def split
 
def splitext
 
def basename
 
def dirname
 
def commonprefix
 
def getsize
 
def getmtime
 
def getatime
 
def islink
 
def exists
 
def isdir
 
def isfile
 
def ismount
 
def walk
 
def expanduser
 
def expandvars
 
def normpath
 
def abspath
 

Variables

list __all__
 
 realpath = abspath
 

Function Documentation

def ntpath.abspath (   path)
Return the absolute version of a path

Definition at line 458 of file ntpath.py.

References normpath().

459 def abspath(path):
460  """Return the absolute version of a path"""
461  if path: # Empty path must return current working directory.
462  from nt import _getfullpathname
463  try:
464  path = _getfullpathname(path)
465  except WindowsError:
466  pass # Bad path - return unchanged.
467  else:
468  path = os.getcwd()
469  return normpath(path)
470 
# realpath is a no-op on systems without islink support
def ntpath.basename (   p)
Returns the final component of a pathname

Definition at line 188 of file ntpath.py.

References split().

189 def basename(p):
190  """Returns the final component of a pathname"""
191  return split(p)[1]
192 
193 
194 # Return the head (dirname) part of a path.
def ntpath.commonprefix (   m)

Definition at line 202 of file ntpath.py.

203 def commonprefix(m):
204  "Given a list of pathnames, returns the longest common leading component"
205  if not m: return ''
206  prefix = m[0]
207  for item in m:
208  for i in range(len(prefix)):
209  if prefix[:i+1] != item[:i+1]:
210  prefix = prefix[:i]
211  if i == 0: return ''
212  break
213  return prefix
214 
215 
216 # Get size, mtime, atime of files.
def ntpath.dirname (   p)
Returns the directory component of a pathname

Definition at line 195 of file ntpath.py.

References split().

196 def dirname(p):
197  """Returns the directory component of a pathname"""
198  return split(p)[0]
199 
200 
201 # Return the longest prefix of all list elements.
def ntpath.exists (   path)
Test whether a path exists

Definition at line 244 of file ntpath.py.

245 def exists(path):
246  """Test whether a path exists"""
247  try:
248  st = os.stat(path)
249  except os.error:
250  return 0
251  return 1
252 
253 
254 # Is a path a dos directory?
255 # This follows symbolic links, so both islink() and isdir() can be true
256 # for the same path.
def ntpath.expanduser (   path)
Expand ~ and ~user constructs.

If user or $HOME is unknown, do nothing.

Definition at line 336 of file ntpath.py.

References join().

337 def expanduser(path):
338  """Expand ~ and ~user constructs.
339 
340  If user or $HOME is unknown, do nothing."""
341  if path[:1] != '~':
342  return path
343  i, n = 1, len(path)
344  while i < n and path[i] not in '/\\':
345  i = i + 1
346  if i == 1:
347  if os.environ.has_key('HOME'):
348  userhome = os.environ['HOME']
349  elif not os.environ.has_key('HOMEPATH'):
350  return path
351  else:
352  try:
353  drive = os.environ['HOMEDRIVE']
354  except KeyError:
355  drive = ''
356  userhome = join(drive, os.environ['HOMEPATH'])
357  else:
358  return path
359  return userhome + path[i:]
360 
361 
362 # Expand paths containing shell variable substitutions.
363 # The following rules apply:
364 # - no expansion within single quotes
365 # - no escape character, except for '$$' which is translated into '$'
366 # - ${varname} is accepted.
367 # - varnames can be made out of letters, digits and the character '_'
368 # XXX With COMMAND.COM you can use any characters in a variable name,
369 # XXX except '^|<>='.
def ntpath.expandvars (   path)
Expand shell variables of form $var and ${var}.

Unknown variables are left unchanged.

Definition at line 370 of file ntpath.py.

371 def expandvars(path):
372  """Expand shell variables of form $var and ${var}.
373 
374  Unknown variables are left unchanged."""
375  if '$' not in path:
376  return path
377  import string
378  varchars = string.ascii_letters + string.digits + '_-'
379  res = ''
380  index = 0
381  pathlen = len(path)
382  while index < pathlen:
383  c = path[index]
384  if c == '\'': # no expansion within single quotes
385  path = path[index + 1:]
386  pathlen = len(path)
387  try:
388  index = path.index('\'')
389  res = res + '\'' + path[:index + 1]
390  except ValueError:
391  res = res + path
392  index = pathlen - 1
393  elif c == '$': # variable or '$$'
394  if path[index + 1:index + 2] == '$':
395  res = res + c
396  index = index + 1
397  elif path[index + 1:index + 2] == '{':
398  path = path[index+2:]
399  pathlen = len(path)
400  try:
401  index = path.index('}')
402  var = path[:index]
403  if os.environ.has_key(var):
404  res = res + os.environ[var]
405  except ValueError:
406  res = res + path
407  index = pathlen - 1
408  else:
409  var = ''
410  index = index + 1
411  c = path[index:index + 1]
412  while c != '' and c in varchars:
413  var = var + c
414  index = index + 1
415  c = path[index:index + 1]
416  if os.environ.has_key(var):
417  res = res + os.environ[var]
418  if c != '':
419  res = res + c
420  else:
421  res = res + c
422  index = index + 1
423  return res
424 
425 
426 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
427 # Previously, this function also truncated pathnames to 8+3 format,
428 # but as this module is called "ntpath", that's obviously wrong!
def ntpath.getatime (   filename)
Return the last access time of a file, reported by os.stat()

Definition at line 227 of file ntpath.py.

228 def getatime(filename):
229  """Return the last access time of a file, reported by os.stat()"""
230  st = os.stat(filename)
231  return st[stat.ST_ATIME]
232 
233 
234 # Is a path a symbolic link?
235 # This will always return false on systems where posix.lstat doesn't exist.
def ntpath.getmtime (   filename)
Return the last modification time of a file, reported by os.stat()

Definition at line 222 of file ntpath.py.

223 def getmtime(filename):
224  """Return the last modification time of a file, reported by os.stat()"""
225  st = os.stat(filename)
226  return st[stat.ST_MTIME]
def ntpath.getsize (   filename)
Return the size of a file, reported by os.stat()

Definition at line 217 of file ntpath.py.

218 def getsize(filename):
219  """Return the size of a file, reported by os.stat()"""
220  st = os.stat(filename)
221  return st[stat.ST_SIZE]
def ntpath.isabs (   s)
Test whether a path is absolute

Definition at line 33 of file ntpath.py.

References splitdrive().

33 
34 def isabs(s):
35  """Test whether a path is absolute"""
36  s = splitdrive(s)[1]
37  return s != '' and s[:1] in '/\\'
38 
39 
40 # Join two (or more) paths.
def ntpath.isdir (   path)
Test whether a path is a directory

Definition at line 257 of file ntpath.py.

References stat.S_ISDIR().

258 def isdir(path):
259  """Test whether a path is a directory"""
260  try:
261  st = os.stat(path)
262  except os.error:
263  return 0
264  return stat.S_ISDIR(st[stat.ST_MODE])
265 
266 
267 # Is a path a regular file?
268 # This follows symbolic links, so both islink() and isdir() can be true
269 # for the same path.
def ntpath.isfile (   path)
Test whether a path is a regular file

Definition at line 270 of file ntpath.py.

References stat.S_ISREG().

271 def isfile(path):
272  """Test whether a path is a regular file"""
273  try:
274  st = os.stat(path)
275  except os.error:
276  return 0
277  return stat.S_ISREG(st[stat.ST_MODE])
278 
279 
280 # Is a path a mount point? Either a root (with or without drive letter)
281 # or an UNC path with at most a / or \ after the mount point.
def ntpath.islink (   path)
Test for symbolic link.  On WindowsNT/95 always returns false

Definition at line 236 of file ntpath.py.

237 def islink(path):
238  """Test for symbolic link. On WindowsNT/95 always returns false"""
239  return 0
240 
241 
242 # Does a path exist?
243 # This is false for dangling symbolic links.
def ntpath.ismount (   path)
Test whether a path is a mount point (defined as root of drive)

Definition at line 282 of file ntpath.py.

References splitdrive(), and splitunc().

283 def ismount(path):
284  """Test whether a path is a mount point (defined as root of drive)"""
285  unc, rest = splitunc(path)
286  if unc:
287  return rest in ("", "/", "\\")
288  p = splitdrive(path)[1]
289  return len(p) == 1 and p[0] in '/\\'
290 
291 
292 # Directory tree walk.
293 # For each directory under top (including top itself, but excluding
294 # '.' and '..'), func(arg, dirname, filenames) is called, where
295 # dirname is the name of the directory and filenames is the list
296 # files files (and subdirectories etc.) in the directory.
297 # The func may modify the filenames list, to implement a filter,
298 # or to impose a different order of visiting.
def ntpath.join (   a,
  p 
)
Join two or more pathname components, inserting "\\" as needed

Definition at line 41 of file ntpath.py.

References isabs().

41 
42 def join(a, *p):
43  """Join two or more pathname components, inserting "\\" as needed"""
44  path = a
45  for b in p:
46  b_wins = 0 # set to 1 iff b makes path irrelevant
47  if path == "":
48  b_wins = 1
49 
50  elif isabs(b):
51  # This probably wipes out path so far. However, it's more
52  # complicated if path begins with a drive letter:
53  # 1. join('c:', '/a') == 'c:/a'
54  # 2. join('c:/', '/a') == 'c:/a'
55  # But
56  # 3. join('c:/a', '/b') == '/b'
57  # 4. join('c:', 'd:/') = 'd:/'
58  # 5. join('c:/', 'd:/') = 'd:/'
59  if path[1:2] != ":" or b[1:2] == ":":
60  # Path doesn't start with a drive letter, or cases 4 and 5.
61  b_wins = 1
62 
63  # Else path has a drive letter, and b doesn't but is absolute.
64  elif len(path) > 3 or (len(path) == 3 and
65  path[-1] not in "/\\"):
66  # case 3
67  b_wins = 1
68 
69  if b_wins:
70  path = b
71  else:
72  # Join, and ensure there's a separator.
73  assert len(path) > 0
74  if path[-1] in "/\\":
75  if b and b[0] in "/\\":
76  path += b[1:]
77  else:
78  path += b
79  elif path[-1] == ":":
80  path += b
81  elif b:
82  if b[0] in "/\\":
83  path += b
84  else:
85  path += "\\" + b
86  else:
87  # path is not empty and does not end with a backslash,
88  # but b is empty; since, e.g., split('a/') produces
89  # ('a', ''), it's best if join() adds a backslash in
90  # this case.
91  path += '\\'
92 
93  return path
94 
95 
96 # Split a path in a drive specification (a drive letter followed by a
97 # colon) and the path specification.
# It is always true that drivespec + pathspec == p
def ntpath.normcase (   s)
Normalize case of pathname.

Makes all characters lowercase and all slashes into backslashes.

Definition at line 20 of file ntpath.py.

References string.lower().

20 
21 def normcase(s):
22  """Normalize case of pathname.
23 
24  Makes all characters lowercase and all slashes into backslashes."""
25  return s.replace("/", "\\").lower()
26 
27 
28 # Return whether a path is absolute.
29 # Trivial in Posix, harder on the Mac or MS-DOS.
30 # For DOS it is absolute if it starts with a slash or backslash (current
31 # volume), or if a pathname after the volume letter and colon / UNC resource
32 # starts with a slash or backslash.
def ntpath.normpath (   path)
Normalize path, eliminating double slashes, etc.

Definition at line 429 of file ntpath.py.

References join(), and splitdrive().

430 def normpath(path):
431  """Normalize path, eliminating double slashes, etc."""
432  path = path.replace("/", "\\")
433  prefix, path = splitdrive(path)
434  while path[:1] == "\\":
435  prefix = prefix + "\\"
436  path = path[1:]
437  comps = path.split("\\")
438  i = 0
439  while i < len(comps):
440  if comps[i] in ('.', ''):
441  del comps[i]
442  elif comps[i] == '..':
443  if i > 0 and comps[i-1] != '..':
444  del comps[i-1:i+1]
445  i -= 1
446  elif i == 0 and prefix.endswith("\\"):
447  del comps[i]
448  else:
449  i += 1
450  else:
451  i += 1
452  # If the path is now empty, substitute '.'
453  if not prefix and not comps:
454  comps.append('.')
455  return prefix + "\\".join(comps)
456 
457 
# Return an absolute path.
def ntpath.split (   p)
Split a pathname.

Return tuple (head, tail) where tail is everything after the final slash.
Either part may be empty.

Definition at line 140 of file ntpath.py.

References splitdrive().

141 def split(p):
142  """Split a pathname.
143 
144  Return tuple (head, tail) where tail is everything after the final slash.
145  Either part may be empty."""
146 
147  d, p = splitdrive(p)
148  # set i to index beyond p's last slash
149  i = len(p)
150  while i and p[i-1] not in '/\\':
151  i = i - 1
152  head, tail = p[:i], p[i:] # now tail has no slashes
153  # remove trailing slashes from head, unless it's all slashes
154  head2 = head
155  while head2 and head2[-1] in '/\\':
156  head2 = head2[:-1]
157  head = head2 or head
158  return d + head, tail
159 
160 
161 # Split a path in root and extension.
162 # The extension is everything starting at the last dot in the last
163 # pathname component; the root is everything before that.
164 # It is always true that root + ext == p.
def ntpath.splitdrive (   p)
Split a pathname into drive and path specifiers. Returns a 2-tuple
"(drive,path)";  either part may be empty

Definition at line 98 of file ntpath.py.

98 
99 def splitdrive(p):
100  """Split a pathname into drive and path specifiers. Returns a 2-tuple
101 "(drive,path)"; either part may be empty"""
102  if p[1:2] == ':':
103  return p[0:2], p[2:]
104  return '', p
105 
106 
# Parse UNC paths
def ntpath.splitext (   p)
Split the extension from a pathname.

Extension is everything from the last dot to the end.
Return (root, ext), either part may be empty.

Definition at line 165 of file ntpath.py.

166 def splitext(p):
167  """Split the extension from a pathname.
168 
169  Extension is everything from the last dot to the end.
170  Return (root, ext), either part may be empty."""
171  root, ext = '', ''
172  for c in p:
173  if c in ['/','\\']:
174  root, ext = root + ext + c, ''
175  elif c == '.':
176  if ext:
177  root, ext = root + ext, c
178  else:
179  ext = c
180  elif ext:
181  ext = ext + c
182  else:
183  root = root + c
184  return root, ext
185 
186 
187 # Return the tail (basename) part of a path.
def ntpath.splitunc (   p)
Split a pathname into UNC mount point and relative path specifiers.

Return a 2-tuple (unc, rest); either part may be empty.
If unc is not empty, it has the form '//host/mount' (or similar
using backslashes).  unc+rest is always the input path.
Paths containing drive letters never have an UNC part.

Definition at line 107 of file ntpath.py.

References normcase().

108 def splitunc(p):
109  """Split a pathname into UNC mount point and relative path specifiers.
110 
111  Return a 2-tuple (unc, rest); either part may be empty.
112  If unc is not empty, it has the form '//host/mount' (or similar
113  using backslashes). unc+rest is always the input path.
114  Paths containing drive letters never have an UNC part.
115  """
116  if p[1:2] == ':':
117  return '', p # Drive letter present
118  firstTwo = p[0:2]
119  if firstTwo == '//' or firstTwo == '\\\\':
120  # is a UNC path:
121  # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
122  # \\machine\mountpoint\directories...
123  # directory ^^^^^^^^^^^^^^^
124  normp = normcase(p)
125  index = normp.find('\\', 2)
126  if index == -1:
127  ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
128  return ("", p)
129  index = normp.find('\\', index + 1)
130  if index == -1:
131  index = len(p)
132  return p[:index], p[index:]
133  return '', p
134 
135 
136 # Split a path in head (everything up to the last '/') and tail (the
137 # rest). After the trailing '/' is stripped, the invariant
138 # join(head, tail) == p holds.
139 # The resulting head won't end in '/' unless it is the root.
def ntpath.walk (   top,
  func,
  arg 
)
Directory tree walk with callback function.

For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
dirname is the name of the directory, and fnames a list of the names of
the files and subdirectories in dirname (excluding '.' and '..').  func
may modify the fnames list in-place (e.g. via del or slice assignment),
and walk will only recurse into the subdirectories whose names remain in
fnames; this can be used to implement a filter, or to impose a specific
order of visiting.  No semantics are defined for, or required of, arg,
beyond that arg is always passed to func.  It can be used, e.g., to pass
a filename pattern, or a mutable object designed to accumulate
statistics.  Passing None for arg is common.

Definition at line 299 of file ntpath.py.

References isdir(), and join().

300 def walk(top, func, arg):
301  """Directory tree walk with callback function.
302 
303  For each directory in the directory tree rooted at top (including top
304  itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
305  dirname is the name of the directory, and fnames a list of the names of
306  the files and subdirectories in dirname (excluding '.' and '..'). func
307  may modify the fnames list in-place (e.g. via del or slice assignment),
308  and walk will only recurse into the subdirectories whose names remain in
309  fnames; this can be used to implement a filter, or to impose a specific
310  order of visiting. No semantics are defined for, or required of, arg,
311  beyond that arg is always passed to func. It can be used, e.g., to pass
312  a filename pattern, or a mutable object designed to accumulate
313  statistics. Passing None for arg is common."""
314 
315  try:
316  names = os.listdir(top)
317  except os.error:
318  return
319  func(arg, top, names)
320  exceptions = ('.', '..')
321  for name in names:
322  if name not in exceptions:
323  name = join(top, name)
324  if isdir(name):
325  walk(name, func, arg)
326 
327 
328 # Expand paths beginning with '~' or '~user'.
329 # '~' means $HOME; '~user' means that user's home directory.
330 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
331 # the path is returned unchanged (leaving error reporting to whatever
332 # function is called with the expanded path as argument).
333 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
334 # (A function should also be defined to do full *sh-style environment
335 # variable expansion.)

Variable Documentation

list __all__
Initial value:
1 = ["normcase","isabs","join","splitdrive","split","splitext",
2  "basename","dirname","commonprefix","getsize","getmtime",
3  "getatime","islink","exists","isdir","isfile","ismount",
4  "walk","expanduser","expandvars","normpath","abspath","splitunc"]

Definition at line 11 of file ntpath.py.

realpath = abspath

Definition at line 471 of file ntpath.py.