This is an autogenerated API Doc for the module "pathutils".
It was generated on: Monday, January 01 06:46 PM.
This module contains convenience functions for working with files and paths.
def bytedivider(nbytes):
Given an integer (probably a long integer returned by os.getsize() ) it returns a tuple of (megabytes, kilobytes, bytes).
This can be more easily converted into a formatted string to display the size of the file.
def formatbytes(sizeint, configdict=None, **configs):
Given a file size as an integer, return a nicely formatted string that represents the size. Has various options to control it's output.
You can pass in a dictionary of arguments or keyword arguments. Keyword arguments override the dictionary and there are sensible defaults for options you don't set.
Options and defaults are as follows :
of kilobytes and bytes only.
1 Mbytes, 307 Kbytes, 478 bytes it outputs using only the largest denominator - e.g. 1.3 Mbytes or 17.2 Kbytes
notice there is no space.
Example outputs :
19Mbytes, 75Kbytes, 255bytes 2Kbytes, 0bytes 23.8Mbytes
Note
It currently uses the plural form even for singular.
def fullcopy(src, dst):
Copy file from src to dst.
If the dst directory doesn't exist, we will attempt to create it using makedirs.
def import_path(fullpath, strict=True):
Import a file from the full path. Allows you to import from anywhere, something __import__ does not do.
If strict is True (the default), raise an ImportError if the module is found in the "wrong" directory.
Taken from firedrop2 by Hans Nowak
def onerror(func, path, exc_info):
Error handler for shutil.rmtree.
If the error is due to an access error (read only file) it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : shutil.rmtree(path, onerror=onerror)
def readbinary(filename):
Given a filename, read a file in binary mode. It returns a single string.
def readfile(filename):
Given a filename, read a file in text mode. It returns a single string.
def readlines(filename):
Passed a filename, it reads it, and returns a list of lines. (Read in text mode)
def relpath(origin, dest):
Return the relative path between origin and dest.
If it's not possible return dest.
If they are identical return os.curdir
Adapted from path.py by Jason Orendorff.
def splitall(loc):
Return a list of the path components in loc. (Used by relpath).
The first item in the list will be either os.curdir, os.pardir, empty, or the root directory of loc (for example, / or ``C:).
The other items in the list will be strings.
Adapted from path.py by Jason Orendorff.
def stringround(main, rest):
Given a file size in either (mb, kb) or (kb, bytes) - round it appropriately.
def tslash(apath):
Add a trailing slash (/) to a path if it lacks one.
It doesn't use os.sep because you end up in trouble on windoze, when you want separators for URLs.
def walkdirs(thisdir):
Walk through all the subdirectories in a tree. Recursively yields directory names (full paths).
def walkemptydirs(thisdir):
Recursively yield names of empty directories.
These are the only paths omitted when using walkfiles.
def walkfiles(thisdir):
walkfiles(D) -> iterator over files in D, recursively. Yields full file paths.
Adapted from path.py by Jason Orendorff.
def writebinary(filename, infile):
Given a filename and a string, write the file in binary mode.
def writefile(filename, infile):
Given a filename and a string, write the file in text mode.
def writelines(filename, infile, newline=False):
Given a filename and a list of lines it writes the file. (In text mode)
If newline is True (default is False) it adds a newline to each line.
class Lock(object):
def __init__(self, filename, timeout=5, step=0.10000000000000001):
A simple file lock, compatible with windows and Unixes.
def lock(self, force=True):
Lock the file for access by creating a directory of the same name (plus a trailing underscore).
The file is only locked if you use this class to acquire the lock before accessing.
If force is True (the default), then on timeout we forcibly acquire the lock.
If force is False, then on timeout a LockError is raised.
def unlock(self, ignore=True):
Release the lock.
If ignore is True and removing the lock directory fails, then the error is surpressed. (This may happen if the lock was acquired via a timeout.)
class LockError(IOError):
The generic error for locking - it is a subclass of IOError.
class LockFile(Lock):
def __init__(self, filename, mode='r', bufsize=-1, timeout=5, step=0.10000000000000001, force=True):
A file like object with an exclusive lock, whilst it is open.
The lock is provided by the Lock class, which creates a directory with the same name as the file (plus a trailing underscore), to indicate that the file is locked.
This is simple and cross platform, with some limitations :
- Unusual process termination could result in the directory being left.
- The process acquiring the lock must have permission to create a directory in the same location as the file.
- It only locks the file against other processes that attempt to acquire a lock using LockFile or Lock.