API Documentation for pathutils

Autogenerated API Docs

Contents

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.

Functions

bytedivider

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.

formatbytes

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 :

  • forcekb = False - If set this forces the output to be in terms

System Message: WARNING/2 (C:\Python Projects\modules in progress\ApiDoc\output\pathutils.txt, line 56)

Bullet list ends without a blank line; unexpected unindent.

of kilobytes and bytes only.

  • largestonly = True - If set, instead of outputting

System Message: WARNING/2 (C:\Python Projects\modules in progress\ApiDoc\output\pathutils.txt, line 59)

Bullet list ends without a blank line; unexpected unindent.
1 Mbytes, 307 Kbytes, 478 bytes it outputs using only the largest denominator - e.g. 1.3 Mbytes or 17.2 Kbytes
  • kiloname = 'Kbytes' - The string to use for kilobytes
  • meganame = 'Mbytes' - The string to use for Megabytes
  • bytename = 'bytes' - The string to use for bytes
  • nospace = True - If set it outputs 1Mbytes, 307Kbytes,

System Message: WARNING/2 (C:\Python Projects\modules in progress\ApiDoc\output\pathutils.txt, line 69)

Bullet list ends without a blank line; unexpected unindent.
notice there is no space.

Example outputs :

19Mbytes, 75Kbytes, 255bytes
2Kbytes, 0bytes
23.8Mbytes

Note

It currently uses the plural form even for singular.

fullcopy

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.

get_main_dir

def get_main_dir():

Return the script directory - whether we're frozen or not.

import_path

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

main_is_frozen

def main_is_frozen():

Return True if we're running from a frozen program.

onerror

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)

readbinary

def readbinary(filename):

Given a filename, read a file in binary mode. It returns a single string.

readfile

def readfile(filename):

Given a filename, read a file in text mode. It returns a single string.

readlines

def readlines(filename):

Passed a filename, it reads it, and returns a list of lines. (Read in text mode)

relpath

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.

splitall

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:).

System Message: WARNING/2 (C:\Python Projects\modules in progress\ApiDoc\output\pathutils.txt, line 206); backlink

Inline literal start-string without end-string.

The other items in the list will be strings.

Adapted from path.py by Jason Orendorff.

stringround

def stringround(main, rest):

Given a file size in either (mb, kb) or (kb, bytes) - round it appropriately.

tslash

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.

walkdirs

def walkdirs(thisdir):

Walk through all the subdirectories in a tree. Recursively yields directory names (full paths).

walkemptydirs

def walkemptydirs(thisdir):

Recursively yield names of empty directories.

These are the only paths omitted when using walkfiles.

walkfiles

def walkfiles(thisdir):

walkfiles(D) -> iterator over files in D, recursively. Yields full file paths.

Adapted from path.py by Jason Orendorff.

writebinary

def writebinary(filename, infile):

Given a filename and a string, write the file in binary mode.

writefile

def writefile(filename, infile):

Given a filename and a string, write the file in text mode.

writelines

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.

Classes

Class Lock

class Lock(object):
    def __init__(self, filename, timeout=5, step=0.10000000000000001):

A simple file lock, compatible with windows and Unixes.

Methods

lock

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.

unlock

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

class LockError(IOError):

The generic error for locking - it is a subclass of IOError.

Class LockFile

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.

Methods

close

def close(self, ignore=True):

close the file and release the lock.

ignore has the same meaning as for Lock.unlock