Python library for interacting with the file system
Project description
Route
Route is a Python library for interacting with the file system in an object oriented manner. I know you can use os and os.path to do all of these but I find their usage hard to remember and true to object oriented principles.
Installation
You can use pip to install Route.
pip install route
Usage
All files and directories (folders) are considered Path objects. Any path except the root, has a parent: the directory it is in. Directories have children, some of them are files and some are subdirectories.
Path
The Path object points to an existing or non-existing file or directory.
from route import Path
path = Path('C:\\')
get_current_directory
Usually we want to start in the current working directory:
path = Path.get_current_directory()
list, dir, ls
All of the above methods do the same thing; ls is for linux people, dir is for windows people, and list is for literal people.
print(path.list())
directories
To get the sub-directories of a directory use the directories attribute which returns a list of Paths:
subdirectories = path.directories
files
To get the files inside a directory use the files attribute which returns a list of Paths:
files = path.files
parent_directory
The parent directory is the directory a Path (either file or directory) is inside of.
parent_directory = path.parent_directory
make_directory
To create a new directory inside a Path that is also a directory, use make_directory with the name of the new directory as the name argument:
path.make_directory(name='new_directory')
You can also create a directory at the location of the Path object by letting the name argument take its default value of None:
path.make_directory()
make_parent_directory
Sometimes you need to create a new file at a Path that doesn't already exist, i.e. the directory of the file location doesn't exist. This is when make_parent_directory becomes handy:
path.make_parent_directory(ignore_if_exists=True)
The default value of ignore_if_exists is True.
delete
The delete function moves a file or directory to the trash. If the name argument is provided the file or directory inside the Path with that name will be deleted:
path.delete(name='new_directory')
If the name argument is not provided the file or directory the Path points to will be deleted:
path.delete()
save
To save a Python object as a Pickle file using the pickle or dill library you can just use the save function of the Path which saves the object right at the location of Path.
my_list = [1, 2, 3]
Path('my_list.pickle').save(method='pickle')
Path('my_list.dill').save(method='dill')
load
To load an object from a Pickle file located at a Path you can run the load function of the Path.
list_from_pickle = Path('my_list.pickle').load(method='pickle')
list_from_dill = Path('my_list.dill').load(method='dill')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file route-0.1.4.tar.gz
.
File metadata
- Download URL: route-0.1.4.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81c49f0e28b3d8dc1271492c4c9dbcdad9396a296b9695dbddbac78d5f52d90b |
|
MD5 | c692aeae7f13316a221b996638d95d37 |
|
BLAKE2b-256 | 11cd924b74c4b14e75a3af23853237cdebf0d5f730be616a3ab07f5bf981011e |
File details
Details for the file route-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: route-0.1.4-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49d87a68e47acfda14356023340ff93190dce3ff0a14fd376358849f6155c033 |
|
MD5 | 4dd1a835661c4e3ceb3cb005d647ac6e |
|
BLAKE2b-256 | 0963da318bdee6f29ff71381d268454337ab0db8f72a5750852a73ba05656d2f |