An easy to use file system wrapper for Python that aims to simplify os, os.path, os.walk, shutils, fnmatch, etc.
This is under active development!
import fs
Reurns True if the path exists. Returns False if path does not exist.
>>> fs.exists('test.txt')
True
>>> fs.exists('some_directory')
True
Reurns True if the path exists and is a file. Returns False if path is a directory or does not exist.
>>> fs.isfile('test.txt')
True
>>> fs.isfile('some_directory')
False
Reurns True if the path exists and is a directory. Returns False if path is a file or does not exist.
>>> fs.isdir('test.txt')
False
>>> fs.isdir('some_directory')
True
Renames oldPath to new newPath where oldPath can be either a file or directory. Raises OSError exception if oldPath does not exist.
>>> fs.rename('old_test.txt', 'new_test.txt')
>>> fs.rename('old_directory', 'new_directory')
Removes all files from the path directory.
>>> fs.truncate('some_directory')
Changes the current directory to path.
>>> fs.chdir('some_directory')
Returns the absolute path from a relative path where path can be either file or directory.
>>> fs.abspath('test.txt')
'/path/to/file/test.txt'
>>> fs.abspath('some_directory')
'/path/to/file/some_directory'
Deletes path where path can be either a file or directory. Raises an OSError exception if path does not exist.
>>> fs.rm('test.txt')
>>> fs.rm('some_directory')
Deletes the file path. Raises an OSError exception if the file does not exist or path is a directory.
>>> fs.unlink('test.txt')
Deletes the directory path with all containing files and directories. Raises an OSError exception if the directory does not exist or path is a file.
>>> fs.rmdir('some_directory')
Sets the timestamp of the file path to the current time or creates the file if path does not exist. Raises an IOError exception if path is a directory.
>>> fs.touch('test.txt')
Returns an array of files and directories that are contained in the directory path. Raises an OSError exception if the directory path does not exist.
>>> fs.list()
['some_directory', 'test.txt']
>>> fs.list('some_directory')
['another_test.txt']
Returns an array of files that are contained in the directory path. Raises an OSError exception if the directory path does not exist.
>>> fs.listfiles()
['test.txt']
>>> fs.listfiles('some_directory')
['another_test.txt']
Example: Loop over all files in the current directory:
>>> for file in fs.listfiles():
pass
Returns an array of directories that are contained in the directory path. Raises an OSError exception if the directory path does not exist.
>>> fs.listdirs()
['some_directory']
>>> fs.listdirs('some_directory')
[]
Example: Loop over all directories in the current directory:
>>> for directory in fs.listdirs():
pass
Returns an array of files that match pattern and are contained in the directory path. Both pattern and exclude can be Unix shell-style wildcards or arrays of wildcards. Raises an OSError exception if the directory path does not exist.
>>> fs.find('*.txt')
['/path/to/file/test.txt', '/path/to/file/some_directory/another_test.txt']
>>> fs.find('*.txt', exclude='another*')
['/path/to/file/test.txt']
Example: Loop over all .csv files in the current directory:
>>> for file in fs.find('*.csv', recursive=False):
pass
Example: Loop over all .xls and .xlsx files in the current directory and all sub-directories:
>>> for file in fs.find(['*.xls', '*.xlsx']):
pass
Example: Loop over all .ini files in the config directory and all sub-directories except the ones starting with local_:
>>> for file in fs.find('*.ini', path='config', exclude='local_*'):
pass
Returns an array of directories that match pattern and are contained in the directory path. Both pattern and exclude can be Unix shell-style wildcards or arrays of wildcards. Raises an OSError exception if the directory path does not exist.
>>> fs.finddirs('some*')
['/path/to/file/some_directory']
>>> fs.finddirs('some*', exclude='*directory')
[]
Returns or sets the content of a file path.
>>> fs.content('text.txt')
u'test'
>>> fs.content('text.txt', 'test')
Returns the content of a file path. Raises an IOError exception if the file path does not exist.
>>> fs.get('text.txt')
u'test'
Sets the content content of a file path.
>>> fs.set('text.txt', 'test')
Appends the content content of a file path. Raises an IOError exception if the file path does not exist.
>>> fs.append('text.txt', 'test')
Joins different parts of a path together.
>>> fs.join('/path/to', 'directory')
Returns or sets the current working directory.
>>> fs.cwd()
'/path/to/directory'
>>> fs.cwd('some_dir')
Returns the extension of a file path.
>>> fs.extension('test.txt')
'txt'
Returns the filename of a file path.
>>> fs.filename('test.txt')
'test'
Returns the directory of a file path.
>>> fs.dirname('/path/to/file/test.txt')
'/path/to/file'
This software is provided under the MIT License.