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 fsReurns True if the path exists. Returns False if path does not exist.
>>> fs.exists('test.txt')
True
>>> fs.exists('some_directory')
TrueReurns 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')
FalseReurns 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')
TrueRenames 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.rmfile('test.txt')The Unix-like fs.unlink is the same as fs.rmfile.
Deletes an array of files paths. Raises an OSError exception if a file does not exist or an element of paths is a directory.
>>> fs.rmfiles(['test.txt', 'another_file.txt'])Example: Remove all files from the current directory:
>>> fs.rmfiles( fs.listfiles() )Example: Remove all .pyc files from a directory:
>>> fs.rmfiles( fs.find('*.pyc') )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')Deletes an array of directories paths with all containing files and directories. Raises an OSError exception if a directory does not exist or an element of paths is a file.
>>> fs.rmdirs(['some_directory', 'some_other_dir'])Example: Remove all directories from the current directory:
>>> fs.rmdirs( fs.listdirs() )Example: Remove all directories that start with local_:
>>> fs.rmdirs( fs.finddirs('local_*') )Sets the modification timestamp of path to the current time or creates the file if path does not exist. Directories not supported on Windows.
>>> fs.touch('test.txt')Generator the returns all files and directories that are contained in the directory path. Raises an OSError exception if the directory path does not exist.
>>> gen = fs.list()
>>> list(gen)
['some_directory', 'test.txt']
>>> gen = fs.list('some_directory')
>>> list(gen)
['another_test.txt']Example: Loop over all files and directories in the current directory:
>>> for f in fs.list():
passGenerator the returns all files that are contained in the directory path. Raises an OSError exception if the directory path does not exist.
>>> gen = fs.listfiles()
>>> list(gen)
['test.txt']
>>> gen = fs.listfiles('some_directory')
>>> list(gen)
['/path/to/dir/some_directory/another_test.txt']Example: Loop over all files in the current directory:
>>> for f in fs.listfiles():
passGenerator the returns all directories that are contained in the directory path. Raises an OSError exception if the directory path does not exist.
>>> gen = fs.listdirs()
>>> list(gen)
['some_directory']
>>> gen = fs.listdirs('some_directory')
>>> list(gen)
[]Example: Loop over all directories in the current directory:
>>> for d in fs.listdirs():
passGenerator the returns all 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.
>>> gen = fs.find('*.txt')
>>> list(gen)
['/path/to/file/test.txt', '/path/to/file/some_directory/another_test.txt']
>>> gen = fs.find('*.txt', exclude='another*')
>>> list(gen)
['/path/to/file/test.txt']Example: Loop over all .csv files in the current directory:
>>> for f in fs.find('*.csv', recursive=False):
passExample: Loop over all .xls and .xlsx files in the current directory and all sub-directories:
>>> for f in fs.find(['*.xls', '*.xlsx']):
passExample: Loop over all .ini files in the config directory and all sub-directories except the ones starting with local_:
>>> for f in fs.find('*.ini', path='config', exclude='local_*'):
passGenerator the returns all 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.
>>> gen = fs.finddirs('some*')
>>> list(gen)
['/path/to/file/some_directory']
>>> gen = fs.finddirs('some*', exclude='*directory')
>>> list(gen)
[]Example: Loop over all .git directories in the current directory and all subdirectories:
>>> for d in fs.find('.git'):
passReturns 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.