Package: pyfolder
PyFolderis a package for managing a filesystem folders as a dictionary.InstallationCurrently it is only supportedPython 3.4.1onwards:sudopip3installpyfolderExample>>>frompyfolderimportPyFolder>>>>>>pyfolder=PyFolder("/path/to/folder")>>>pyfolder["file.txt"]="hello, this is going to be instantly the content of this file."Basic UsagePyFoldercan easily store or read content from the filesystem. The usage is the same as a normal dictionary:Create a file with specific binary content:>>>frompyfolderimportPyFolder>>>>>>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"}PyFolderautomatically 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 flagauto_create_folderis set toFalseduring 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 defaultPyFolderwill 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 flaginterpret=Falseduring instantiation:>>>pyfolder=PyFolder("/path/to/folder",interpret=False)Edit content:PyFolderwon’t allow modification or removal of elements unless the flagallow_overrideis specified during instantiation:>>>pyfolder=PyFolder("/path/to/folder",allow_override=True)>>>pyfolder['file.bin']=b"replaced_content_bytes"Remove content:>>>delpyfolder['file.bin']Note that a folder can also be removed:>>>delpyfolder['folder1']>>>delpyfolder['.']# deletes PyFolder root folderBy default PyFolder won’t remove a folder unless its content is empty. In order to be able to remove folders without restriction, enable the flagallow_remove_folders_with_content>>>pyfolder=PyFolder("/path/to/folder",allow_remove_folders_with_content=True)Iterate over the files:By defaultPyFolderallows iteration over files, including the folders:>>>forfile_nameinpyfolder:>>>print(file_name)If it is wanted to access also the content, it can be done with theitems()method:>>>forfile_name,contentinpyfolder.items():>>>print(file_name,content)If only files are wanted, thefiles()method exists to serve the purpose:>>>forfile_nameinpyfolder.files()...>>>forfile_name,contentinpyfolder.files_items()Iterate over folders:>>>forfolder_nameinpyfolder.folders():...it is also possible to iterate over the folder name and its content at the same time:>>>forfolder_name,folder_contentinpyfolder.folders_items():...InPyFolder, each folder is aPyFolderobject. It is perfectly possible to nest folders as follows:>>>pyfolder["folder1"]["folder2"]>>>pyfolder["folder1/folder2"]# Equivalent in relative URI notationSearch for files:PyFoldereases 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']LICENSEIt is released under the MIT license.