Python's filesystem abstraction layer
Python’s Filesystem abstraction layer.
Think of PyFilesystem’s FS objects as the next logical step to Python’s file objects. In the same way that file objects abstract a single file, FS objects abstract an entire filesystem.
Let’s look at a simple piece of code as an example. The following function uses the PyFilesystem API to count the number of non-blank lines of Python code in a directory. It works recursively, so it will find .py files in all sub-directories.
def count_python_loc(fs): """Count non-blank lines of Python code.""" count = 0 for path in fs.walk.files(filter=['*.py']): with fs.open(path) as python_file: count += sum(1 for line in python_file if line.strip()) return count
We can call count_python_loc as follows:
from fs import open_fs projects_fs = open_fs('~/projects') print(count_python_loc(projects_fs))
The line project_fs = open_fs('~/projects') opens an FS object that maps to the projects directory in your home folder. That object is used by count_python_loc when counting lines of code.
To count the lines of Python code in a zip file, we can make the following change:
projects_fs = open_fs('zip://projects.zip')
Or to count the Python lines on an FTP server:
projects_fs = open_fs('ftp://ftp.example.org/projects')
No changes to count_python_loc are necessary, because PyFileystem provides a simple consistent interface to anything that resembles a collection of files and directories. Essentially, it allows you to write code that is independent of where and how the files are physically stored.
Contrast that with a version that purely uses the standard library:
def count_py_loc(path): count = 0 for root, dirs, files in os.walk(path): for name in files: if name.endswith('.py'): with open(os.path.join(root, name), 'rt') as python_file: count += sum(1 for line in python_file if line.strip())
This version is similar to the PyFilesystem code above, but would only work with the OS filesystem. Any other filesystem would require an entirely different API, and you would likely have to re-implement the directory walking functionality of os.walk.
Martin Larralde for TarFS
Giampaolo for copy_if_newer and ftp fixes.
PyFilesystem2 owes a massive debt of gratitude to the following developers who contributed code and ideas to the original version.
Apologies if I missed anyone, feel free to prompt me if your name is missing here.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for fs-2.0.10a2-py2.py3-none-any.whl