Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python's Filesystem abstraction layer

License

NotificationsYou must be signed in to change notification settings

PyFilesystem/pyfilesystem2

Repository files navigation

Python's Filesystem abstraction layer.

PyPI versionPyPIDownloadsBuild StatusWindows Build StatusCoverage StatusCodacy BadgeDocs

Documentation

Introduction

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.

Credits

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.

Support

If commercial support is required, please contactWill McGugan.

Packages

No packages published

Contributors43


[8]ページ先頭

©2009-2025 Movatter.jp