Package elisa :: Package extern :: Module path :: Class path
[hide private]
[frames] | no frames]

Class path

source code


Represents a filesystem path.

For documentation on individual methods, consult their counterparts in os.path.

Instance Methods [hide private]
 
__add__(self, more)
x+y
source code
 
__radd__(self, other) source code
 
__div__(self, rel)
fp.__div__(rel) == fp / rel == fp.joinpath(rel)
source code
 
__truediv__(self, rel)
fp.__div__(rel) == fp / rel == fp.joinpath(rel)
source code
 
isabs(s)
Test whether a path is absolute
 
abspath(self) source code
 
normcase(self) source code
 
normpath(self) source code
 
realpath(self) source code
 
expanduser(self) source code
 
expandvars(self) source code
 
dirname(self) source code
 
basename(p)
Returns the final component of a pathname
 
expand(self)
Clean up a filename by calling expandvars(), expanduser(), and normpath() on it.
source code
 
_get_namebase(self) source code
 
_get_ext(self) source code
 
_get_drive(self) source code
 
splitpath(p)
Returns: Return (p.parent, p.name)
source code
 
splitdrive(p)
Split the drive specifier from this path.
source code
 
splitext(p)
Split the filename extension from this path and return the two parts.
source code
 
stripext(p)
For example, path('/home/guido/python.tar.gz').stripext() returns path('/home/guido/python.tar').
source code
 
splitunc(self) source code
 
_get_uncshare(self) source code
 
joinpath(self, *args)
Join two or more path components, adding a separator character (os.sep) if needed.
source code
 
splitall(self)
Return a list of the path components in this path.
source code
 
relpath(self)
Return this path as a relative path, based from the current working directory.
source code
 
relpathto(self, dest)
Return a relative path from self to dest.
source code
 
listdir(D)
Use D.files() or D.dirs() instead if you want a listing of just files or just subdirectories.
source code
 
dirs(D)
The elements of the list are path objects.
source code
 
files(D)
The elements of the list are path objects.
source code
 
walk(D)
The iterator yields path objects naming each child item of this directory and its descendants.
source code
 
walkdirs(D)
With the optional 'pattern' argument, this yields only directories whose names match the given pattern.
source code
 
walkfiles(D)
The optional argument, pattern, limits the results to files with names that match the pattern.
source code
 
fnmatch(self, pattern)
Return True if self.name matches the given pattern.
source code
 
glob(self, pattern)
Return a list of path objects that match the pattern.
source code
 
open(self, mode='r')
Open this file.
source code
 
bytes(self)
Open this file, read all bytes, return them as a string.
source code
 
write_bytes(self, bytes, append=True)
Open this file and write the given bytes to it.
source code
 
text(self, encoding='frontend', errors='strict')
Open this file, read it in, return the content as a string.
source code
 
write_text(self, text, encoding='frontend', errors='strict', linesep='\n', append=True)
Write the given text to this file.
source code
 
lines(self, encoding='frontend', errors='strict', retain=True)
Open this file, read all lines, return them in a list.
source code
 
write_lines(self, lines, encoding='frontend', errors='strict', linesep='\n', append=True)
Write the given lines of text to this file.
source code
 
read_md5(self)
Calculate the md5 hash for this file.
source code
 
exists(path)
Test whether a path exists.
 
isdir(path)
Test whether a path is a directory
 
isfile(path)
Test whether a path is a regular file
 
islink(path)
Test whether a path is a symbolic link
 
ismount(path)
Test whether a path is a mount point
 
samefile(f1, f2)
Test whether two pathnames reference the same actual file
 
getatime(filename)
Return the last access time of a file, reported by os.stat().
 
getmtime(filename)
Return the last modification time of a file, reported by os.stat().
 
getctime(filename)
Return the metadata change time of a file, reported by os.stat().
 
getsize(filename)
Return the size of a file, reported by os.stat().
 
access(self, mode)
Return true if current user has access to this path.
source code
 
stat(self)
Perform a stat() system call on this path.
source code
 
lstat(self)
Like path.stat(), but do not follow symbolic links.
source code
 
get_owner(self)
Return the name of the owner of this file or directory.
source code
 
statvfs(self)
Perform a statvfs() system call on this path.
source code
 
pathconf(self, name) source code
 
utime(self, times)
Set the access and modified times of this file.
source code
 
chmod(self, mode) source code
 
chown(self, uid, gid) source code
 
rename(self, new) source code
 
renames(self, new) source code
 
mkdir(self, mode=511) source code
 
makedirs(self, mode=511) source code
 
rmdir(self) source code
 
removedirs(self) source code
 
touch(self)
Set the access/modified times of this file to the current time.
source code
 
remove(self) source code
 
unlink(self) source code
 
link(self, newpath)
Create a hard link at 'newpath', pointing to this file.
source code
 
symlink(self, newlink)
Create a symbolic link at 'newlink', pointing here.
source code
 
readlink(self)
Return the path to which this symbolic link points.
source code
 
readlinkabs(self)
Return the path to which this symbolic link points.
source code
 
copyfile(src, dst)
Copy data from src to dst
 
copymode(src, dst)
Copy mode bits from src to dst
 
copystat(src, dst)
Copy all stat info (mode bits, atime and mtime) from src to dst
 
copy(src, dst)
Copy data and mode bits ("cp src dst").
 
copy2(src, dst)
Copy data and all stat info ("cp -p src dst").
 
copytree(src, dst, symlinks=True)
Recursively copy a directory tree using copy2().
 
move(src, dst)
Recursively move a file or directory to another location.
 
rmtree(path, ignore_errors=True, onerror='frontend')
Recursively delete a directory tree.
 
chroot(self) source code
 
startfile(self) source code

Inherited from unicode: __contains__, __eq__, __ge__, __getattribute__, __getitem__, __getnewargs__, __getslice__, __gt__, __hash__, __le__, __len__, __lt__, __mod__, __mul__, __ne__, __new__, __repr__, __rmod__, __rmul__, __str__, capitalize, center, count, decode, encode, endswith, expandtabs, find, index, isalnum, isalpha, isdecimal, isdigit, islower, isnumeric, isspace, istitle, isupper, join, ljust, lower, lstrip, partition, replace, rfind, rindex, rjust, rpartition, rsplit, rstrip, split, splitlines, startswith, strip, swapcase, title, translate, upper, zfill

Inherited from object: __delattr__, __init__, __reduce__, __reduce_ex__, __setattr__

Class Methods [hide private]
 
getcwd(cls)
Return the current working directory as a path object.
source code
Class Variables [hide private]
  uncshare = property(_get_uncshare, None, None, """ The UNC mou...
Properties [hide private]
  parent
This path's parent directory, as a new path object.
  name
The name of this file or directory without the full path.
  namebase
The same as path.name, but with one file extension stripped off.
  ext
The file extension, for example '.py'.
  drive
The drive specifier, for example 'C:'.
  atime
Last access time of the file.
  mtime
Last-modified time of the file.
  ctime
Creation time of the file.
  size
Size of the file, in bytes.
  owner
Name of the owner of this file or directory.

Inherited from object: __class__

Method Details [hide private]

__add__(self, more)
(Addition operator)

source code 
x+y
Overrides: unicode.__add__
(inherited documentation)

__div__(self, rel)

source code 

fp.__div__(rel) == fp / rel == fp.joinpath(rel)

Join two path components, adding a separator character if needed.

__truediv__(self, rel)

source code 

fp.__div__(rel) == fp / rel == fp.joinpath(rel)

Join two path components, adding a separator character if needed.

expand(self)

source code 

Clean up a filename by calling expandvars(), expanduser(), and normpath() on it.

This is commonly everything needed to clean up a filename read from a configuration file, for example.

splitpath(p)

source code 
Returns:
Return (p.parent, p.name)

splitdrive(p)

source code 
Split the drive specifier from this path. If there is no drive specifier, p.drive is empty, so the return value is simply (path(''), p). This is always the case on Unix.
Returns:
Return (p.drive, <the rest of p>)

splitext(p)

source code 

Split the filename extension from this path and return the two parts. Either part may be empty.

The extension is everything from '.' to the end of the last path segment. This has the property that if (a, b) == p.splitext(), then a + b == p.
Returns:
Return (p.stripext(), p.ext)

stripext(p)

source code 
For example, path('/home/guido/python.tar.gz').stripext() returns path('/home/guido/python.tar').
Returns:
Remove one file extension from the path

joinpath(self, *args)

source code 
Join two or more path components, adding a separator character (os.sep) if needed. Returns a new path object.

splitall(self)

source code 

Return a list of the path components in this path.

The first item in the list will be a path. Its value will be either os.curdir, os.pardir, empty, or the root directory of this path (for example, '/' or 'C:\\'). The other items in the list will be strings.

path.path.joinpath(*result) will yield the original path.

relpathto(self, dest)

source code 

Return a relative path from self to dest.

If there is no relative path from self to dest, for example if they reside on different drives in Windows, then this returns dest.abspath().

listdir(D)

source code 

Use D.files() or D.dirs() instead if you want a listing of just files or just subdirectories.

The elements of the list are path objects.

With the optional 'pattern' argument, this only lists items whose names match the given pattern.
Returns:
List of items in this directory

dirs(D)

source code 

The elements of the list are path objects. This does not walk recursively into subdirectories (but see path.walkdirs).

With the optional 'pattern' argument, this only lists directories whose names match the given pattern. For example, d.dirs('build-*').
Returns:
List of this directory's subdirectories

files(D)

source code 

The elements of the list are path objects. This does not walk into subdirectories (see path.walkfiles).

With the optional 'pattern' argument, this only lists files whose names match the given pattern. For example, d.files('*.pyc').
Returns:
List of the files in this directory

walk(D)

source code 

The iterator yields path objects naming each child item of this directory and its descendants. This requires that D.isdir().

This performs a depth-first traversal of the directory tree. Each directory is returned just before all its children.

The errors= keyword argument controls behavior when an error occurs. The default is 'strict', which causes an exception. The other allowed values are 'warn', which reports the error via warnings.warn(), and 'ignore'.
Returns:
iterator over files and subdirs, recursively

walkdirs(D)

source code 

With the optional 'pattern' argument, this yields only directories whose names match the given pattern. For example, mydir.walkdirs('*test') yields only directories with names ending in 'test'.

The errors= keyword argument controls behavior when an error occurs. The default is 'strict', which causes an exception. The other allowed values are 'warn', which reports the error via warnings.warn(), and 'ignore'.
Returns:
iterator over subdirs, recursively

walkfiles(D)

source code 
The optional argument, pattern, limits the results to files with names that match the pattern. For example, mydir.walkfiles('*.tmp') yields only files with the .tmp extension.
Returns:
iterator over files in D, recursively

fnmatch(self, pattern)

source code 
Return True if self.name matches the given pattern.

pattern - A filename pattern with wildcards,
    for example '*.py'.

glob(self, pattern)

source code 

Return a list of path objects that match the pattern.

pattern - a path relative to this directory, with wildcards.

For example, path('/users').glob('*/bin/*') returns a list of all the files users have in their bin directories.

open(self, mode='r')

source code 
Open this file. Return a file object.

write_bytes(self, bytes, append=True)

source code 

Open this file and write the given bytes to it.

Default behavior is to overwrite any existing file. Call p.write_bytes(bytes, append=True) to append instead.

text(self, encoding='frontend', errors='strict')

source code 
Open this file, read it in, return the content as a string.

This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r'
are automatically translated to '\n'.

Optional arguments:

encoding - The Unicode encoding (or character set) of
    the file.  If present, the content of the file is
    decoded and returned as a unicode object; otherwise
    it is returned as an 8-bit str.
errors - How to handle Unicode errors; see help(str.decode)
    for the options.  Default is 'strict'.

write_text(self, text, encoding='frontend', errors='strict', linesep='\n', append=True)

source code 

Write the given text to this file.

The default behavior is to overwrite any existing file; to append instead, use the 'append=True' keyword argument.

There are two differences between path.write_text() and path.write_bytes(): newline handling and Unicode handling. See below.

Parameters:
  • text - str/unicode - The text to be written.
  • encoding - str - The Unicode encoding that will be used. This is ignored if 'text' isn't a Unicode string.
  • errors - str - How to handle Unicode encoding errors. Default is 'strict'. See help(unicode.encode) for the options. This is ignored if 'text' isn't a Unicode string.
  • linesep - keyword argument - str/unicode - The sequence of characters to be used to mark end-of-line. The default is os.linesep. You can also specify None; this means to leave all newlines as they are in 'text'.
  • append - keyword argument - bool - Specifies what to do if the file already exists (True: append to the end of it; False: overwrite it.) The default is False.

--- Newline handling.

write_text() converts all standard end-of-line sequences ('\n', '\r', and '\r\n') to your platform's default end-of-line sequence (see os.linesep; on Windows, for example, the end-of-line marker is '\r\n').

If you don't like your platform's default, you can override it using the 'linesep=' keyword argument. If you specifically want write_text() to preserve the newlines as-is, use 'linesep=None'.

This applies to Unicode text the same as to 8-bit text, except there are three additional standard Unicode end-of-line sequences: u'\x85', u'\r\x85', and u'\u2028'.

(This is slightly different from when you open a file for writing with fopen(filename, "w") in C or file(filename, 'w') in Python.)

--- Unicode

If 'text' isn't Unicode, then apart from newline handling, the bytes are written verbatim to the file. The 'encoding' and 'errors' arguments are not used and must be omitted.

If 'text' is Unicode, it is first converted to bytes using the specified 'encoding' (or the default encoding if 'encoding' isn't specified). The 'errors' argument applies only to this conversion.

lines(self, encoding='frontend', errors='strict', retain=True)

source code 
Open this file, read all lines, return them in a list.

Optional arguments:
    encoding - The Unicode encoding (or character set) of
        the file.  The default is None, meaning the content
        of the file is read as 8-bit characters and returned
        as a list of (non-Unicode) str objects.
    errors - How to handle Unicode errors; see help(str.decode)
        for the options.  Default is 'strict'
    retain - If true, retain newline characters; but all newline
        character combinations ('\r', '\n', '\r\n') are
        translated to '\n'.  If false, newline characters are
        stripped off.  Default is True.

This uses 'U' mode in Python 2.3 and later.

write_lines(self, lines, encoding='frontend', errors='strict', linesep='\n', append=True)

source code 
Write the given lines of text to this file.

By default this overwrites any existing file at this path.

This puts a platform-specific newline sequence on every line.
See 'linesep' below.

lines - A list of strings.

encoding - A Unicode encoding to use.  This applies only if
    'lines' contains any Unicode strings.

errors - How to handle errors in Unicode encoding.  This
    also applies only to Unicode strings.

linesep - The desired line-ending.  This line-ending is
    applied to every line.  If a line already has any
    standard line ending ('\r', '\n', '\r\n', u'\x85',
    u'\r\x85', u'\u2028'), that will be stripped off and
    this will be used instead.  The default is os.linesep,
    which is platform-dependent ('\r\n' on Windows, '\n' on
    Unix, etc.)  Specify None to write the lines as-is,
    like file.writelines().

Use the keyword argument append=True to append lines to the
file.  The default is to overwrite the file.  Warning:
When you use this with Unicode data, if the encoding of the
existing data in the file is different from the encoding
you specify with the encoding= parameter, the result is
mixed-encoding data, which can really confuse someone trying
to read the file later.

read_md5(self)

source code 

Calculate the md5 hash for this file.

This reads through the entire file.

exists(path)

 
Test whether a path exists. Returns False for broken symbolic links

access(self, mode)

source code 

Return true if current user has access to this path.

mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK

get_owner(self)

source code 

Return the name of the owner of this file or directory.

This follows symbolic links.

On Windows, this returns a name of the form ur'DOMAIN\User Name'. On Windows, a group can own a file or directory.

touch(self)

source code 
Set the access/modified times of this file to the current time. Create the file if it does not exist.

readlink(self)

source code 

Return the path to which this symbolic link points.

The result may be an absolute or a relative path.

readlinkabs(self)

source code 

Return the path to which this symbolic link points.

The result is always an absolute path.

copy(src, dst)

 

Copy data and mode bits ("cp src dst").

The destination may be a directory.

copy2(src, dst)

 

Copy data and all stat info ("cp -p src dst").

The destination may be a directory.

copytree(src, dst, symlinks=True)

 

Recursively copy a directory tree using copy2().

The destination directory must not already exist. If exception(s) occur, an Error is raised with a list of reasons.

If the optional symlinks flag is true, symbolic links in the source tree result in symbolic links in the destination tree; if it is false, the contents of the files pointed to by symbolic links are copied.

XXX Consider this example code rather than the ultimate tool.

move(src, dst)

 

Recursively move a file or directory to another location.

If the destination is on our current filesystem, then simply use rename. Otherwise, copy src to the dst and then remove src. A lot more could be done here... A look at a mv.c shows a lot of the issues this implementation glosses over.

rmtree(path, ignore_errors=True, onerror='frontend')

 

Recursively delete a directory tree.

If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is os.listdir, os.remove, or os.rmdir; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised.

Class Variable Details [hide private]

uncshare

Value:
property(_get_uncshare, None, None, """ The UNC mount point for this p\
ath.
            This is empty for paths on local drives. """)

Property Details [hide private]

parent

This path's parent directory, as a new path object.

For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib')
Get Method:
elisa.extern.path.path.dirname(self)
Set Method:
'frontend'
Delete Method:
'frontend'

name

The name of this file or directory without the full path.

For example, path('/usr/local/lib/libpython.so').name == 'libpython.so'
Get Method:
posixpath.basename(p) - Returns the final component of a pathname
Set Method:
'frontend'
Delete Method:
'frontend'

namebase

The same as path.name, but with one file extension stripped off.

For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz', but path('/home/guido/python.tar.gz').namebase == 'python.tar'
Get Method:
elisa.extern.path.path._get_namebase(self)
Set Method:
'frontend'
Delete Method:
'frontend'

ext

The file extension, for example '.py'.
Get Method:
elisa.extern.path.path._get_ext(self)
Set Method:
'frontend'
Delete Method:
'frontend'

drive

The drive specifier, for example 'C:'. This is always empty on systems that don't use drive specifiers.
Get Method:
elisa.extern.path.path._get_drive(self)
Set Method:
'frontend'
Delete Method:
'frontend'

atime

Last access time of the file.
Get Method:
posixpath.getatime(filename) - Return the last access time of a file, reported by os.stat().
Set Method:
'frontend'
Delete Method:
'frontend'

mtime

Last-modified time of the file.
Get Method:
posixpath.getmtime(filename) - Return the last modification time of a file, reported by os.stat().
Set Method:
'frontend'
Delete Method:
'frontend'

ctime

Creation time of the file.
Get Method:
posixpath.getctime(filename) - Return the metadata change time of a file, reported by os.stat().
Set Method:
'frontend'
Delete Method:
'frontend'

size

Size of the file, in bytes.
Get Method:
posixpath.getsize(filename) - Return the size of a file, reported by os.stat().
Set Method:
'frontend'
Delete Method:
'frontend'

owner

Name of the owner of this file or directory.
Get Method:
elisa.extern.path.path.get_owner(self) - Return the name of the owner of this file or directory.
Set Method:
'frontend'
Delete Method:
'frontend'