Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork181
Python's Filesystem abstraction layer
License
PyFilesystem/pyfilesystem2
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Python's Filesystem abstraction layer.
Wiki(currently offline)- API Documentation
- GitHub Repository
- Blog
Think of PyFilesystem'sFS
objects as the next logical step toPython'sfile
objects. In the same way that file objects abstract asingle file, FS objects abstract an entire filesystem.
Let's look at a simple piece of code as an example. The followingfunction uses the PyFilesystem API to count the number of non-blanklines of Python code in a directory. It worksrecursively, so it willfind.py
files in all sub-directories.
defcount_python_loc(fs):"""Count non-blank lines of Python code."""count=0forpathinfs.walk.files(filter=['*.py']):withfs.open(path)aspython_file:count+=sum(1forlineinpython_fileifline.strip())returncount
We can callcount_python_loc
as follows:
fromfsimportopen_fsprojects_fs=open_fs('~/projects')print(count_python_loc(projects_fs))
The lineproject_fs = open_fs('~/projects')
opens an FS object thatmaps to theprojects
directory in your home folder. That object isused bycount_python_loc
when counting lines of code.
To count the lines of Python code in azip file, we can make thefollowing 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 tocount_python_loc
are necessary, because PyFileystemprovides a simple consistent interface to anything that resembles acollection of files and directories. Essentially, it allows you to writecode that is independent of where and how the files are physicallystored.
Contrast that with a version that purely uses the standard library:
defcount_py_loc(path):count=0forroot,dirs,filesinos.walk(path):fornameinfiles:ifname.endswith('.py'):withopen(os.path.join(root,name),'rt')aspython_file:count+=sum(1forlineinpython_fileifline.strip())returncount
This version is similar to the PyFilesystem code above, but would onlywork with the OS filesystem. Any other filesystem would require anentirely different API, and you would likely have to re-implement thedirectory walking functionality ofos.walk
.
The following developers have contributed code and their time to this projects:
SeeCONTRIBUTORS.mdfor a full list of contributors.
PyFilesystem2 owes a massive debt of gratitude to the followingdevelopers who contributed code and ideas to the original version.
- Ryan Kelly
- Andrew Scheller
- Ben Timby
Apologies if I missed anyone, feel free to prompt me if your name ismissing here.
If commercial support is required, please contactWill McGugan.
About
Python's Filesystem abstraction layer
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.