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

Functions

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

Variables

list __all__
 
 _varprog = None
 

Detailed Description

Common operations on Posix pathnames.

Instead of importing this module directly, import os and refer to
this module as os.path.  The "os.path" name is an alias for this
module on Posix systems; on other systems (e.g. Mac, Windows),
os.path provides the same operations in a manner specific to that
platform, and is an alias to another module (e.g. macpath, ntpath).

Some of this can actually be useful on non-Posix systems too, e.g.
for manipulation of the pathname component of URLs.

Function Documentation

def posixpath.abspath (   path)
Return an absolute path.

Definition at line 387 of file posixpath.py.

References isabs(), join(), and normpath().

388 def abspath(path):
389  """Return an absolute path."""
390  if not isabs(path):
391  path = join(os.getcwd(), path)
392  return normpath(path)
393 
394 
395 # Return a canonical path (i.e. the absolute location of a file on the
396 # filesystem).
def posixpath.basename (   p)
Returns the final component of a pathname

Definition at line 108 of file posixpath.py.

References split().

109 def basename(p):
110  """Returns the final component of a pathname"""
111  return split(p)[1]
112 
113 
114 # Return the head (dirname) part of a path.
def posixpath.commonprefix (   m)

Definition at line 122 of file posixpath.py.

123 def commonprefix(m):
124  "Given a list of pathnames, returns the longest common leading component"
125  if not m: return ''
126  prefix = m[0]
127  for item in m:
128  for i in range(len(prefix)):
129  if prefix[:i+1] != item[:i+1]:
130  prefix = prefix[:i]
131  if i == 0: return ''
132  break
133  return prefix
134 
135 
136 # Get size, mtime, atime of files.
def posixpath.dirname (   p)
Returns the directory component of a pathname

Definition at line 115 of file posixpath.py.

References split().

116 def dirname(p):
117  """Returns the directory component of a pathname"""
118  return split(p)[0]
119 
120 
121 # Return the longest prefix of all list elements.
def posixpath.exists (   path)
Test whether a path exists.  Returns false for broken symbolic links

Definition at line 168 of file posixpath.py.

169 def exists(path):
170  """Test whether a path exists. Returns false for broken symbolic links"""
171  try:
172  st = os.stat(path)
173  except os.error:
174  return 0
175  return 1
176 
177 
178 # Is a path a directory?
179 # This follows symbolic links, so both islink() and isdir() can be true
180 # for the same path.
def posixpath.expanduser (   path)
Expand ~ and ~user constructions.  If user or $HOME is unknown,
do nothing.

Definition at line 299 of file posixpath.py.

300 def expanduser(path):
301  """Expand ~ and ~user constructions. If user or $HOME is unknown,
302  do nothing."""
303  if path[:1] != '~':
304  return path
305  i, n = 1, len(path)
306  while i < n and path[i] != '/':
307  i = i + 1
308  if i == 1:
309  if not os.environ.has_key('HOME'):
310  return path
311  userhome = os.environ['HOME']
312  else:
313  import pwd
314  try:
315  pwent = pwd.getpwnam(path[1:i])
316  except KeyError:
317  return path
318  userhome = pwent[5]
319  if userhome[-1:] == '/': i = i + 1
320  return userhome + path[i:]
321 
322 
323 # Expand paths containing shell variable substitutions.
324 # This expands the forms $variable and ${variable} only.
325 # Non-existent variables are left unchanged.
def posixpath.expandvars (   path)
Expand shell variables of form $var and ${var}.  Unknown variables
are left unchanged.

Definition at line 328 of file posixpath.py.

329 def expandvars(path):
330  """Expand shell variables of form $var and ${var}. Unknown variables
331  are left unchanged."""
332  global _varprog
333  if '$' not in path:
334  return path
335  if not _varprog:
336  import re
337  _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
338  i = 0
339  while 1:
340  m = _varprog.search(path, i)
341  if not m:
342  break
343  i, j = m.span(0)
344  name = m.group(1)
345  if name[:1] == '{' and name[-1:] == '}':
346  name = name[1:-1]
347  if os.environ.has_key(name):
348  tail = path[j:]
349  path = path[:i] + os.environ[name]
350  i = len(path)
351  path = path + tail
352  else:
353  i = j
354  return path
355 
356 
357 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
358 # It should be understood that this may change the meaning of the path
359 # if it contains symbolic links!
def posixpath.getatime (   filename)
Return the last access time of a file, reported by os.stat().

Definition at line 147 of file posixpath.py.

148 def getatime(filename):
149  """Return the last access time of a file, reported by os.stat()."""
150  st = os.stat(filename)
151  return st[stat.ST_ATIME]
152 
153 
154 # Is a path a symbolic link?
155 # This will always return false on systems where os.lstat doesn't exist.
def posixpath.getmtime (   filename)
Return the last modification time of a file, reported by os.stat().

Definition at line 142 of file posixpath.py.

143 def getmtime(filename):
144  """Return the last modification time of a file, reported by os.stat()."""
145  st = os.stat(filename)
146  return st[stat.ST_MTIME]
def posixpath.getsize (   filename)
Return the size of a file, reported by os.stat().

Definition at line 137 of file posixpath.py.

138 def getsize(filename):
139  """Return the size of a file, reported by os.stat()."""
140  st = os.stat(filename)
141  return st[stat.ST_SIZE]
def posixpath.isabs (   s)
Test whether a path is absolute

Definition at line 35 of file posixpath.py.

35 
36 def isabs(s):
37  """Test whether a path is absolute"""
38  return s[:1] == '/'
39 
40 
41 # Join pathnames.
42 # Ignore the previous parts if a part is absolute.
43 # Insert a '/' unless the first part is empty or already ends in '/'.
def posixpath.isdir (   path)
Test whether a path is a directory

Definition at line 181 of file posixpath.py.

References stat.S_ISDIR().

182 def isdir(path):
183  """Test whether a path is a directory"""
184  try:
185  st = os.stat(path)
186  except os.error:
187  return 0
188  return stat.S_ISDIR(st[stat.ST_MODE])
189 
190 
191 # Is a path a regular file?
192 # This follows symbolic links, so both islink() and isfile() can be true
193 # for the same path.
def posixpath.isfile (   path)
Test whether a path is a regular file

Definition at line 194 of file posixpath.py.

References stat.S_ISREG().

195 def isfile(path):
196  """Test whether a path is a regular file"""
197  try:
198  st = os.stat(path)
199  except os.error:
200  return 0
201  return stat.S_ISREG(st[stat.ST_MODE])
202 
203 
204 # Are two filenames really pointing to the same file?
def posixpath.islink (   path)
Test whether a path is a symbolic link

Definition at line 156 of file posixpath.py.

References stat.S_ISLNK().

157 def islink(path):
158  """Test whether a path is a symbolic link"""
159  try:
160  st = os.lstat(path)
161  except (os.error, AttributeError):
162  return 0
163  return stat.S_ISLNK(st[stat.ST_MODE])
164 
165 
166 # Does a path exist?
167 # This is false for dangling symbolic links.
def posixpath.ismount (   path)
Test whether a path is a mount point

Definition at line 234 of file posixpath.py.

References join().

235 def ismount(path):
236  """Test whether a path is a mount point"""
237  try:
238  s1 = os.stat(path)
239  s2 = os.stat(join(path, '..'))
240  except os.error:
241  return 0 # It doesn't exist -- so not a mount point :-)
242  dev1 = s1[stat.ST_DEV]
243  dev2 = s2[stat.ST_DEV]
244  if dev1 != dev2:
245  return 1 # path/.. on a different device as path
246  ino1 = s1[stat.ST_INO]
247  ino2 = s2[stat.ST_INO]
248  if ino1 == ino2:
249  return 1 # path/.. is the same i-node as path
250  return 0
251 
252 
253 # Directory tree walk.
254 # For each directory under top (including top itself, but excluding
255 # '.' and '..'), func(arg, dirname, filenames) is called, where
256 # dirname is the name of the directory and filenames is the list
257 # of files (and subdirectories etc.) in the directory.
258 # The func may modify the filenames list, to implement a filter,
259 # or to impose a different order of visiting.
def posixpath.join (   a,
  p 
)
Join two or more pathname components, inserting '/' as needed

Definition at line 44 of file posixpath.py.

44 
45 def join(a, *p):
46  """Join two or more pathname components, inserting '/' as needed"""
47  path = a
48  for b in p:
49  if b[:1] == '/':
50  path = b
51  elif path == '' or path[-1:] == '/':
52  path = path + b
53  else:
54  path = path + '/' + b
55  return path
56 
57 
58 # Split a path in head (everything up to the last '/') and tail (the
59 # rest). If the path ends in '/', tail will be empty. If there is no
60 # '/' in the path, head will be empty.
61 # Trailing '/'es are stripped from head unless it is the root.
def posixpath.normcase (   s)
Normalize case of pathname.  Has no effect under Posix

Definition at line 27 of file posixpath.py.

27 
28 def normcase(s):
29  """Normalize case of pathname. Has no effect under Posix"""
30  return s
31 
32 
33 # Return whether a path is absolute.
34 # Trivial in Posix, harder on the Mac or MS-DOS.
def posixpath.normpath (   path)
Normalize path, eliminating double slashes, etc.

Definition at line 360 of file posixpath.py.

References join().

361 def normpath(path):
362  """Normalize path, eliminating double slashes, etc."""
363  if path == '':
364  return '.'
365  initial_slashes = path.startswith('/')
366  # POSIX allows one or two initial slashes, but treats three or more
367  # as single slash.
368  if (initial_slashes and
369  path.startswith('//') and not path.startswith('///')):
370  initial_slashes = 2
371  comps = path.split('/')
372  new_comps = []
373  for comp in comps:
374  if comp in ('', '.'):
375  continue
376  if (comp != '..' or (not initial_slashes and not new_comps) or
377  (new_comps and new_comps[-1] == '..')):
378  new_comps.append(comp)
379  elif new_comps:
380  new_comps.pop()
381  comps = new_comps
382  path = '/'.join(comps)
383  if initial_slashes:
384  path = '/'*initial_slashes + path
385  return path or '.'
386 
def posixpath.realpath (   filename)
Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path.

Definition at line 397 of file posixpath.py.

References abspath(), islink(), join(), normpath(), and split().

398 def realpath(filename):
399  """Return the canonical path of the specified filename, eliminating any
400 symbolic links encountered in the path."""
401  filename = abspath(filename)
402 
403  bits = ['/'] + filename.split('/')[1:]
404  for i in range(2, len(bits)+1):
405  component = join(*bits[0:i])
406  if islink(component):
407  resolved = os.readlink(component)
408  (dir, file) = split(component)
409  resolved = normpath(join(dir, resolved))
410  newpath = join(*([resolved] + bits[i:]))
411  return realpath(newpath)
412 
413  return filename
def posixpath.samefile (   f1,
  f2 
)
Test whether two pathnames reference the same actual file

Definition at line 205 of file posixpath.py.

References samestat().

206 def samefile(f1, f2):
207  """Test whether two pathnames reference the same actual file"""
208  s1 = os.stat(f1)
209  s2 = os.stat(f2)
210  return samestat(s1, s2)
211 
212 
213 # Are two open files really referencing the same file?
214 # (Not necessarily the same file descriptor!)
def posixpath.sameopenfile (   fp1,
  fp2 
)
Test whether two open file objects reference the same file

Definition at line 215 of file posixpath.py.

References samestat().

216 def sameopenfile(fp1, fp2):
217  """Test whether two open file objects reference the same file"""
218  s1 = os.fstat(fp1)
219  s2 = os.fstat(fp2)
220  return samestat(s1, s2)
221 
222 
223 # Are two stat buffers (obtained from stat, fstat or lstat)
224 # describing the same file?
def posixpath.samestat (   s1,
  s2 
)
Test whether two stat buffers reference the same file

Definition at line 225 of file posixpath.py.

226 def samestat(s1, s2):
227  """Test whether two stat buffers reference the same file"""
228  return s1[stat.ST_INO] == s2[stat.ST_INO] and \
229  s1[stat.ST_DEV] == s2[stat.ST_DEV]
230 
231 
232 # Is a path a mount point?
233 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
def posixpath.split (   p)
Split a pathname.  Returns tuple "(head, tail)" where "tail" is
everything after the final slash.  Either part may be empty.

Definition at line 62 of file posixpath.py.

62 
63 def split(p):
64  """Split a pathname. Returns tuple "(head, tail)" where "tail" is
65  everything after the final slash. Either part may be empty."""
66  i = p.rfind('/') + 1
67  head, tail = p[:i], p[i:]
68  if head and head != '/'*len(head):
69  while head[-1] == '/':
70  head = head[:-1]
71  return head, tail
72 
73 
74 # Split a path in root and extension.
75 # The extension is everything starting at the last dot in the last
76 # pathname component; the root is everything before that.
77 # It is always true that root + ext == p.
def posixpath.splitdrive (   p)
Split a pathname into drive and path. On Posix, drive is always
empty.

Definition at line 100 of file posixpath.py.

101 def splitdrive(p):
102  """Split a pathname into drive and path. On Posix, drive is always
103  empty."""
104  return '', p
105 
106 
107 # Return the tail (basename) part of a path.
def posixpath.splitext (   p)
Split the extension from a pathname.  Extension is everything from the
last dot to the end.  Returns "(root, ext)", either part may be empty.

Definition at line 78 of file posixpath.py.

78 
79 def splitext(p):
80  """Split the extension from a pathname. Extension is everything from the
81  last dot to the end. Returns "(root, ext)", either part may be empty."""
82  root, ext = '', ''
83  for c in p:
84  if c == '/':
85  root, ext = root + ext + c, ''
86  elif c == '.':
87  if ext:
88  root, ext = root + ext, c
89  else:
90  ext = c
91  elif ext:
92  ext = ext + c
93  else:
94  root = root + c
95  return root, ext
96 
97 
98 # Split a pathname into a drive specification and the rest of the
99 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
def posixpath.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 260 of file posixpath.py.

References join(), and stat.S_ISDIR().

261 def walk(top, func, arg):
262  """Directory tree walk with callback function.
263 
264  For each directory in the directory tree rooted at top (including top
265  itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
266  dirname is the name of the directory, and fnames a list of the names of
267  the files and subdirectories in dirname (excluding '.' and '..'). func
268  may modify the fnames list in-place (e.g. via del or slice assignment),
269  and walk will only recurse into the subdirectories whose names remain in
270  fnames; this can be used to implement a filter, or to impose a specific
271  order of visiting. No semantics are defined for, or required of, arg,
272  beyond that arg is always passed to func. It can be used, e.g., to pass
273  a filename pattern, or a mutable object designed to accumulate
274  statistics. Passing None for arg is common."""
275 
276  try:
277  names = os.listdir(top)
278  except os.error:
279  return
280  func(arg, top, names)
281  for name in names:
282  name = join(top, name)
283  try:
284  st = os.lstat(name)
285  except os.error:
286  continue
287  if stat.S_ISDIR(st[stat.ST_MODE]):
288  walk(name, func, arg)
289 
290 
291 # Expand paths beginning with '~' or '~user'.
292 # '~' means $HOME; '~user' means that user's home directory.
293 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
294 # the path is returned unchanged (leaving error reporting to whatever
295 # function is called with the expanded path as argument).
296 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
297 # (A function should also be defined to do full *sh-style environment
298 # 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",
5  "samefile","sameopenfile","samestat"]

Definition at line 16 of file posixpath.py.

_varprog = None

Definition at line 326 of file posixpath.py.