Module ll.fileutils
A module that provides some tools for file i/o related stuff.
The first is a class Filename, that
provides several methods (e.g. a method open,
that automatically creates any non exiting directories).
The second is a class named SafeFile,
that encapsulates a normal file. Nothing will be written to the file with the
specified name. All writes go to a temporary file instead. Only when the
method close is called, will the
temporary file be closed and the file renamed to the original name.
The third tool is a class LogFile. Writing
to a log file prepends a timestamp to each line.
Classes
class Filename(UserString.UserString):
def __init__(self, name):
selfcreate a new Filename instance.
name may be a Filename
instance or a string.
def __repr__(self):
selfdef __hash__(self):
selfdef __cmp__(self, other):
selfdef __div__(self, other):
selfcontatenate two filenames. other may be a
Filename or a string.
def cd(self, other):
selfsynonym for self/other.
def clone(self):
selfreturn a (deep) copy of self.
def ext(self):
selfreturn the file extension.
def files(self, withDir=0):
selfreturn a list of files in this directory (and all subdirectories)
def getmtime(self):
selfreturn the time of the last modification timestamp to the file object.
def mkdir(self):
selfcreate a directory with the name of self.
def mkparentdir(self):
selfcreate the parent directory of self.
def open(self, mode='rb', buffering=-1):
selfWorks like the builtin function open, but is able to create non existing directories.
def openread(self, headers=None, data=None):
selfdef openwrite(self):
selfdef remove(self):
selfdelete the file
def rename(self, other):
selfrename the file. other must be a Filename instance.
def url(self):
selfreturn self as an URL (i.e. with a leading file:.
def withExt(self, ext):
selfreturn a copy of self with the new extension ext.
def withoutExt(self):
selfreturn a copy of self without an extension.
def __walk(self, withDirfiles, dirname, filenames):
selfinternal method used by files
class LogFile:
A log file. All lines written to the file will be prepended with a time stamp.
def __init__(self, name, mode='a', buffering=1, encoding='iso-8859-1'):
selfcreate a new log file (which will be opened on the first write). Arguments are:
name: the filename (either as a string or aFilenameinstance).mode: The mode for opening the file (should be"w"or"a")buffering: the buffering for the file (0is unbuffered,1is line buffered, any other integer specifies the buffersize)encoding: the encoding to use for the strings written to the file
def write(self, *texts):
selfwrite texts to the log file.
def __open(self):
selfdef _formatTime(self, timestamp):
selfformat timestamp into a string.
class SafeFile:
encapsulates a normal file. When the file will be opened, all writes go to a temporary file. After all writes are finished the file will be renamed to its final name.
def __init__(self, name, mode='w', buffering=0):
selfdef close(self):
selfThis method can be called by the user when all writes to the file are done. The file will be closed and moved to the final destination.
def write(self, *texts):
selfdef writelines(self, *alllines):
selfdef __open(self):
selfFunctions
def cwd():
return the current working directory as a Filename.
def getFirst(names):
Return the first file or directory from the sequence names that does exist.
None entries will be ignored. If none of the names exists None will be
returned.
