Skip to main content

PyFolder is a package for managing a filesystem folders as a dictionary.

https://badge.fury.io/py/pyfolder.svg https://travis-ci.org/ipazc/pyfolder.svg?branch=master https://coveralls.io/repos/github/ipazc/pyfolder/badge.svg?branch=master Code Health

Installation

Currently it is only supported Python 3.4.1 onwards:

sudo pip3 install pyfolder

Example

>>> from pyfolder import PyFolder
>>>
>>> pyfolder = PyFolder("/path/to/folder")
>>> pyfolder["file.txt"] = "hello, this is going to be instantly the content of this file."

Basic Usage

PyFolder can easily store or read content from the filesystem. The usage is the same as a normal dictionary:

  • Create a file with specific binary content:

>>> from pyfolder import PyFolder
>>>
>>> pyfolder = PyFolder("/path/to/folder")
>>> pyfolder['file.bin'] = b"Content as bytes"
>>> pyfolder['file.txt'] = "Content as text"
>>> pyfolder['file.json'] = {"content": "Content as JSON"}

PyFolder automatically detects the kind of content to store.

It is also possible to reference the creation of a file in relative file URI notation:

>>> pyfolder["folder1/folder2/file.txt"] = "content"

If folder specified doesn’t exist, by default it will be created automatically unless the flag auto_create_folder is set to False during instantiation:

>>> pyfolder = PyFolder("/path/to/folder", auto_create_folder=False)

Note that “.” or “..” chars are not allowed in URI notation, it must be relative URIs to the root.

  • Get specific content:

>>> pyfolder = PyFolder("/path/to/folder")
>>> pyfolder['file.bin']
b"Content as bytes"
>>> pyfolder['file.txt']
"Content as text"
>>> pyfolder['file.json']
{"content": "Content as JSON"}
>>> pyfolder['folder1/folder2/file.bin']
b"Other content"

By default PyFolder will attempt to load the content with the best interpreter it has, based on the file extension. If no interpreter is found for a content, it will return the content in bytes format. This behaviour can be disabled with the flag interpret=False during instantiation:

>>> pyfolder = PyFolder("/path/to/folder", interpret=False)
  • Edit content:

PyFolder won’t allow modification or removal of elements unless the flag allow_override is specified during instantiation:

>>> pyfolder = PyFolder("/path/to/folder", allow_override=True)
>>> pyfolder['file.bin'] = b"replaced_content_bytes"
  • Remove content:

>>> del pyfolder['file.bin']

Note that a folder can also be removed:

>>> del pyfolder['folder1']
>>> del pyfolder['.']  # deletes PyFolder root folder

By default PyFolder won’t remove a folder unless its content is empty. In order to be able to remove folders without restriction, enable the flag allow_remove_folders_with_content

>>> pyfolder = PyFolder("/path/to/folder", allow_remove_folders_with_content=True)
  • Iterate over the files:

By default PyFolder allows iteration over files, including the folders:

>>> for file_name in pyfolder:
>>>    print(file_name)

If it is wanted to access also the content, it can be done with the items() method:

>>> for file_name, content in pyfolder.items():
>>>    print(file_name, content)

If only files are wanted, the files() method exists to serve the purpose:

>>> for file_name in pyfolder.files()
...
>>> for file_name, content in pyfolder.files_items()
  • Iterate over folders:

>>> for folder_name in pyfolder.folders():
...

it is also possible to iterate over the folder name and its content at the same time:

>>> for folder_name, folder_content in pyfolder.folders_items():
...

In PyFolder, each folder is a PyFolder object. It is perfectly possible to nest folders as follows:

>>> pyfolder["folder1"]["folder2"]
>>> pyfolder["folder1/folder2"]  # Equivalent in relative URI notation
  • Search for files:

PyFolder eases the search of a file/folder by matching a name. It will return the list of relative URIs of the file-names found:

>>> pyfolder.index("name.bin")
>>> ['path/to/name.bin', 'path2/to/name.bin']

LICENSE

It is released under the MIT license.

Metadata

Release files for pyfolder 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfolder 0.0.2
File Size Uploaded
pyfolder-0.0.2.tar.gz 9.5 kB Details

Release files / pyfolder-0.0.2.tar.gz

Download URL pyfolder-0.0.2.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7a3c0fb1e6387939881d419fbd891036cc55cfa04cede704ff2f58120a27b002
BLAKE2b-256 checksum
How to use checksums
1b0f8e997f6a735b9e59af76eb3c292106503d40ccc2b9662e817914ef8b114a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page