Table of Contents
A collection file related general purpose routines.
Note
That find is a recursive search and findindir is a search within one directory.
Takes the path and replaces its extension with new_ext. The new_ext should include the dot ("."). For example,
would have "c" equal to "test.doc".
Performs a recursive search for a filename pattern.
Much like typing "dir .txt" will give you a listing of all the files with the extension "txt", this function will return a listing of all the files that match the given pattern (i.e. ".txt" or "silly").
Notice that this function will recursively traverse from the startdir.
predicate = lambda path: fnmatch.fnmatch(path, pattern) return filter(predicate, os.listdir(dir))
Returns the files found in a directory with the given pattern. The search is non-recursive.
Takes a path and returns a the directory portion.
Assumes that if the path is just a filename, then the file resides in the local directory. Also, if the path is just a directory, it will return that directory back again. For example, consider we are in c:foobardirectory, then the following inputs would be,
blah.dat -> c:foobar c:foobar -> c:foobar c:foobarblah.dat -> c:foobar . -> c:foobar "" -> c:foobar
Determines if a path is read only or not.
Assumes that the path is actually pointing to a file (satisfying os.path.isfile()), if it is not, then behavior is undefined.
This method only works on win32 machines, if you are running a non-win32 machine, this method will throw a RuntimeError.
Takes a basename and an extension and makes a file name (with the ".").
Just makes the code cleaner than using [basename]+"."+[extension]. Assumes both basename and extension are strings.
Searches supplied path for file name and returns 1 if file is in the path. The search is non-recursive.
Returns a formatted error message to be displayed by the calling script when file is not found in path or has no associated spawning application.
Formatted string:
- [parent].py Error: [fln]
- Incorrect path or path has no associated application. Please contact your System Administrator.
Evaluates the path and returns the path with the exact same case pattern as on the disk. For example, say your file, on the disk was TestFile.dat. If you ran,
you would get back TestFile.dat.
Will find all occurrences in path that a regular file resides.
New in 4.60.
The lockfile class is a file-like object that builds in locking mechanics. If you use the lockfile class to access a file on the disk, it will ensure that you can only have one reader/writer at a time.
You can open the object with the same path and mode rules as the standard file object. All other file object methods are also available (read, write, flush, etc.).
The lockfile class allows for different types of lock_type values.
- Exclusive (LOCK_EX): This means all other lock requests will block until the original lock is released. This means that if there is a lock on a file, an exclusive lock will not return until the lock has been released.
- Shared (LOCK_SH): This will allow "read" requests to the file, but not write requests.
- Non-Blocking (LOCK_NB): This will not block on a locked file, and will generate an fileutils.LockError immediately.
The locking mechanics only apply if you work with the lockfile object to access the same file. If you attempt to open a locked file using the standard file object, you will get undefined results.
An example of using the lock file is as follows,
# Acquire an EXCLUSIVE lock on the file. If any other process had a lock
# on the file, this call would block until a lock was released.
fp = fileutils.lockfile(path, "wt", lock_type=fileutils.LOCK_EX)
fp.write("test")
# Still holding onto the lock, we will attempt to acquire another
# lock, but this time it will be non-blocking. For demo purposes
# we will only try 10 times.
cnt = 0
while cnt < 10:
try:
fp_o = fileutils.lockfile(path, "wt", lock_type=fileutils.LOCK_NB)
except fileutils.LockError, e:
print str(cnt) + str(e)
cnt += 1
fp.close() # This releases the orginal lock!
the above example illustrates how you can ensure that only one file at a time locks the code.
The original code was inspired by the portalocker [3] recipe on ActiveState.
ReadOnlyFileLike(data)
This object emulates a file-like interface in a read-only mode. This is useful if you have a chunk of in memory data that you want to keep in memory, but you want to supply a file-like interface.
A good example of this is the python zipfile module, which requires a file-like interface to unpack the data. If your "zip file" is in memory, you can wrap it with the ReadOnlyFileLike and it will let you "unpack in memory".
binary_data = load_from_somewhere()
filelike = fileutils.ReadOnlyFileLike(binary_data)
zfile = zipfile.ZipFile(filelike, mode='r')
# Do zip uncompressionin the above example, the ZipFile module will parse the filelike object as if it where a file.
This only provides "read" interfaces. If you want a "write" version, you should use the StringIO module provided by python.
[1] | Lutz, Mark. Programming Python. 2nd Ed. 2001 (O'Reilly). |
[2] | "os.path -- Common pathname manipulations." Python Library Reference. 21 December 2001. 12 May 2004. <http://www.python.org/doc/2.2/lib/module-os.path.html> |
[3] | Jonathan Feinberg. "Recipe 65203: portalocker - Cross-platform (posix/nt) API for flock-style file locking." ActiveState Code. 16 May 2008. 18 Feburary 2009. ' |