Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfolder-0.0.2.tar.gz (9.5 kB view details)

Uploaded Source

File details

Details for the file pyfolder-0.0.2.tar.gz.

File metadata

  • Download URL: pyfolder-0.0.2.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyfolder-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7a3c0fb1e6387939881d419fbd891036cc55cfa04cede704ff2f58120a27b002
MD5 af8a8371efd574a54d64a37ac4dd283e
BLAKE2b-256 1b0f8e997f6a735b9e59af76eb3c292106503d40ccc2b9662e817914ef8b114a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page