Home  >  Python software  >  FileUtils

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

create a new Filename instance. name may be a Filename instance or a string.

def __repr__(self):

def __hash__(self):

def __cmp__(self, other):

def __div__(self, other):

contatenate two filenames. other may be a Filename or a string.

def cd(self, other):

synonym for self/other.

def clone(self):

return a (deep) copy of self.

def ext(self):

return the file extension.

def files(self, withDir=0):

return a list of files in this directory (and all subdirectories)

def getmtime(self):

return the time of the last modification timestamp to the file object.

def mkdir(self):

create a directory with the name of self.

def mkparentdir(self):

create the parent directory of self.

def open(self, mode='rb', buffering=-1):

Works like the builtin function open, but is able to create non existing directories.

def openread(self, headers=None, data=None):

def openwrite(self):

def remove(self):

delete the file

def rename(self, other):

rename the file. other must be a Filename instance.

def url(self):

return self as an URL (i.e. with a leading file:.

def withExt(self, ext):

return a copy of self with the new extension ext.

def withoutExt(self):

return a copy of self without an extension.

def __walk(self, withDirfiles, dirname, filenames):

internal 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'):

create a new log file (which will be opened on the first write). Arguments are:

  • name: the filename (either as a string or a Filename instance).
  • mode: The mode for opening the file (should be "w" or "a")
  • buffering: the buffering for the file (0 is unbuffered, 1 is line buffered, any other integer specifies the buffersize)
  • encoding: the encoding to use for the strings written to the file

def write(self, *texts):

write texts to the log file.

def __open(self):

def _formatTime(self, timestamp):

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

def close(self):

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

def writelines(self, *alllines):

def __open(self):

Functions

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.

def timestamp2dt(t):