Introduction

Let's look at a simple use-case. The following function uses the PyFilesystem API to count the number of non-blank lines of Python code in a directory (and any subdirectories):

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

The above code can be made to work with any filesystem, here are just a few examples:

# HD with open_fs('.') as fs: print(count_python_loc(fs))

# Zip file with open_fs('zip://projects.zip') as fs: print(count_python_loc(fs))

# FTP Server with open_fs('ftp://ftp.example.org/projects') as fs: print(count_python_loc(fs))

Contrast that with the equivalent code using just the standard library:

def count_python_loc(path): """Count non-blank lines of Python code.""" 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()) return count