This module contains the basic FS interface and a number of other essential interfaces.
All Filesystem objects inherit from this class.
The base class for Filesystem abstraction objects. An instance of a class derived from FS is an abstraction on some kind of filesystem, such as the OS filesystem or a zip file.
The base class for Filesystem objects.
| Parameters: |
|
|---|
Displays the FS tree in a graphical window (requires wxPython)
| Parameters: |
|
|---|
| Parameters: |
|
|---|
| Parameters: |
|
|---|
Close the filesystem. This will perform any shutdown related operations required. This method will be called automatically when the filesystem object is garbage collected, but it is good practice to call it explicitly so that any attached resourced are freed when they are no longer required.
Copies a file from src to dst.
| Parameters: |
|
|---|
copies a directory from one location to another.
| Parameters: |
|
|---|
Creates an empty file if it doesn’t exist
| Parameters: |
|
|---|
Returns short descriptive text regarding a path. Intended mainly as a debugging aid.
| Parameters: |
|
|---|---|
| Return type: | str |
Check if a path references a valid resource.
| Parameters: |
|
|---|---|
| Return type: | bool |
Returns the contents of a file as a string.
| Parameters: |
|
|---|---|
| Return type: | str |
| Returns: | file contents |
Returns information for a path as a dictionary. The exact content of this dictionary will vary depending on the implementation, but will likely include a few common values. The following values will be found in info dictionaries for most implementations:
- “size” - Number of bytes used to store the file or directory
- “created_time” - A datetime object containing the time the resource was created
- “accessed_time” - A datetime object containing the time the resource was last accessed
- “modified_time” - A datetime object containing the time the resource was modified
| Parameters: |
|
|---|---|
| Return type: | dict |
| Raises: |
|
Retrieve a meta value associated with an FS object.
Meta values are a way for an FS implementation to report potentially useful information associated with the file system.
A meta key is a lower case string with no spaces. Meta keys may also be grouped in namespaces in a dotted notation, e.g. ‘atomic.namespaces’. FS implementations aren’t obliged to return any meta values, but the following are common:
- read_only True if the file system cannot be modified
- thread_safe True if the implementation is thread safe
- network True if the file system requires network access
- unicode_paths True if the file system supports unicode paths
- case_insensitive_paths True if the file system ignores the case of paths
- atomic.makedir True if making a directory is an atomic operation
- atomic.rename True if rename is an atomic operation, (and not implemented as a copy followed by a delete)
- atomic.setcontents True if the implementation supports setting the contents of a file as an atomic operation (without opening a file)
- free_space The free space (in bytes) available on the file system
- total_space The total space (in bytes) available on the file system
- virtual True if the filesystem defers to other filesystems
FS implementations may expose non-generic meta data through a self-named namespace. e.g. "somefs.some_meta"
Since no meta value is guaranteed to exist, it is advisable to always supply a default value to getmeta.
| Parameters: |
|
|---|---|
| Raises fs.errors.NoMetaError: | |
If specified meta value is not present, and there is no default |
|
Returns a mmap object for this path.
See http://docs.python.org/library/mmap.html for more details on the mmap module.
| Parameters: |
|
|---|---|
| Raises fs.errors.NoMMapError: | |
Only paths that have a syspath can be opened as a mmap |
|
Returns a url that corresponds to the given path, if one exists.
If the path does not have an equivalent URL form (and allow_none is False) then a NoPathURLError exception is thrown. Otherwise the URL will be returns as an unicode string.
| Parameters: |
|
|---|---|
| Raises fs.errors.NoPathURLError: | |
If no URL form exists, and allow_none is False (the default) |
|
| Return type: | unicode |
Returns the size (in bytes) of a resource.
| Parameters: |
|
|---|---|
| Returns: | the size of the file |
| Return type: | integer |
Returns the system path (a path recognized by the OS) if one is present.
If the path does not map to a system path (and allow_none is False) then a NoSysPathError exception is thrown. Otherwise, the system path will be returned as a unicode string.
| Parameters: |
|
|---|---|
| Raises fs.errors.NoSysPathError: | |
if the path does not map on to a system path, and allow_none is set to False (default) |
|
| Return type: | unicode |
Check that a meta value is supported
| Parameters: |
|
|---|---|
| Return type: | bool |
Check if the path has an equivalent URL form
| Parameters: |
|
|---|---|
| Returns: | True if path has a URL form |
| Return type: | bool |
Check if the path maps to a system path (a path recognized by the OS).
| Parameters: |
|
|---|---|
| Returns: | True if path maps to a system path |
| Return type: | bool |
Generator yielding the files and directories under a given path.
This method behaves identically to fs.base.FS.listdir() but returns an generator instead of a list. Depending on the filesystem this may be more efficient than calling fs.base.FS.listdir() and iterating over the resulting list.
Generator yielding paths and path info under a given path.
This method behaves identically to listdirinfo() but returns an generator instead of a list. Depending on the filesystem this may be more efficient than calling listdirinfo() and iterating over the resulting list.
Check if a path references a directory.
| Parameters: |
|
|---|---|
| Return type: | bool |
Check if a directory is empty (contains no files or sub-directories)
| Parameters: |
|
|---|---|
| Return type: | bool |
Check if a path references a file.
| Parameters: |
|
|---|---|
| Return type: | bool |
Lists the the files and directories under a given path.
The directory contents are returned as a list of unicode paths.
| Parameters: |
|
|---|---|
| Return type: | iterable of paths |
| Raises: |
|
Retrieves a list of paths and path info under a given path.
This method behaves like listdir() but instead of just returning the name of each item in the directory, it returns a tuple of the name and the info dict as returned by getinfo.
This method may be more efficient than calling getinfo() on each individual item returned by listdir(), particularly for network based filesystems.
| Parameters: |
|
|---|---|
| Raises: |
|
Make a directory on the filesystem.
| Parameters: |
|
|---|---|
| Raises: |
|
makes a directory (if it doesn’t exist) and returns an FS object for the newly created directory.
| Parameters: |
|
|---|---|
| Returns: | the opened dir |
| Return type: | an FS object |
moves a file from one location to another.
| Parameters: |
|
|---|---|
| Raises fs.errors.DestinationExistsError: | |
if destination exists and overwrite is False |
|
moves a directory from one location to another.
| Parameters: |
|
|---|---|
| Raises fs.errors.DestinationExistsError: | |
if destination exists and overwrite is False |
|
Open a the given path as a file-like object.
| Parameters: |
|
|---|---|
| Return type: | a file-like object |
| Raises: |
|
Opens a directory and returns a FS object representing its contents.
| Parameters: |
|
|---|---|
| Returns: | the opened dir |
| Return type: | an FS object |
Prints a tree structure of the FS object to the console
| Parameters: |
|
|---|
Remove a file from the filesystem.
| Parameters: |
|
|---|---|
| Raises: |
|
Remove a directory from the filesystem
| Parameters: |
|
|---|---|
| Raises: |
|
Renames a file or directory
| Parameters: |
|
|---|---|
| Raises: |
|
Like open(), but returns a NullFile if the file could not be opened.
A NullFile is a dummy file which has all the methods of a file-like object, but contains no data.
| Parameters: |
|
|---|---|
| Return type: | a file-like object |
A convenience method to create a new file from a string or file-like object
| Parameters: |
|
|---|
Create a new file from a string or file-like object asynchronously
This method returns a threading.Event object. Call the wait method on the event object to block until all data has been written, or simply ignore it.
| Parameters: |
|
|---|---|
| Returns: | An event object that is set when the copy is complete, call the wait method of this object to block until the data is written |
Set the accessed time and modified time of a file
| Parameters: |
|
|---|
Prints a tree structure of the FS object to the console
| Parameters: |
|
|---|
Walks a directory tree and yields the root path and contents. Yields a tuple of the path of each directory and a list of its file contents.
| Parameters: |
|
|---|---|
| Return type: | iterator of (current_path, paths) |
Like the ‘walk’ method but yields directories.
| Parameters: |
|
|---|---|
| Return type: | iterator of dir paths |
Like the ‘walk’ method, but just yields file paths.
| Parameters: |
|
|---|---|
| Return type: | iterator of file paths |
A SubFS is an FS implementation that represents a directory on another Filesystem. When you use the fs.base.FS.opendir() method it will return a SubFS instance. You should not need to instantiate a SubFS directly.
For example:
from fs.osfs import OSFS
home_fs = OSFS('foo')
bar_fs = home_fs.opendir('bar')
A NullFile is a file-like object with no functionality. It is used in situations where a file-like object is required but the caller doesn’t have any data to read or write.
The fs.base.FS.safeopen() method returns an NullFile instance, which can reduce error-handling code.
For example, the following code may be written to append some text to a log file:
logfile = None
try:
logfile = myfs.open('log.txt', 'a')
logfile.writeline('operation successful!')
finally:
if logfile is not None:
logfile.close()
This could be re-written using the safeopen method:
myfs.safeopen('log.txt', 'a').writeline('operation successful!')
If the file doesn’t exist then the call to writeline will be a null-operation (i.e. not do anything).