Skip to main content

droot: Python File Management Package

droot is a Python package that provides a convenient way to manage files and directories. It allows you to perform various operations such as creating, deleting, copying, moving, and iterating over files and directories.

Installation

You can install droot using pip:

pip install droot

Usage

Importing the Package

To use droot, you need to import the necessary classes:

from droot import DRoot, DFile

Creating a Directory Root

You can create a directory root using the DRoot class. Pass the absolute path of the directory to the constructor:

root = DRoot('/path/to/root')

Creating a File

To create a file, you can use the DFile class. Pass the absolute path of the file and the file name to the constructor:

file = DFile('/path/to/root', 'example.txt')

Checking Existence

You can check if a directory or file exists using the bool() function:

if bool(root):
    print('Root directory exists')

if bool(file):
    print('File exists')

File Operations

Reading a File

To read the contents of a file, you can use the left shift operator (<<):

content = file << ''
print(content)

Writing to a File

To write content to a file, you can use the left shift operator (<<):

file << 'Hello, world!'

Appending to a File

To append content to a file, you can use the in-place left shift operator (<<=):

file <<= ' Appended content'

Moving a File

To move a file to a different location, you can use the mv() method:

new_file = file.mv('/path/to/new_location/example.txt')

Copying a File

To copy a file to a different location, you can use the cp() method:

new_file = file.cp('/path/to/new_location/example.txt')

Deleting a File

To delete a file, you can use the unlink() method:

file.unlink()

Directory Operations

Creating a Directory

To create a directory, you can use the makedirs() method:

root.makedirs()

Creating a Directory and Clearing its Contents

To create a directory and clear its contents if it already exists, you can use the makeAndClear() method:

root.makeAndClear()

Listing Files and Directories

To iterate over files and directories within a directory, you can use a for loop:

for item in root:
    print(item)

The item variable will be either a DFile object or a DRoot object, depending on whether it represents a file or a directory.

Accessing a Subdirectory

To access a subdirectory within a directory, you can use the indexing operator ([]):

subdir = root['subdirectory']

Getting the Parent Directory

To get the parent directory of a directory or file, you can use the parent property:

parent_dir = root.parent

Getting the Relative Path

To get the relative path between two files or directories, you can use the subtraction operator (-):

relative_path = file - root

Printing the Directory Tree

To print the directory tree structure, you can use the tree() method:

tree = root.tree()
for item in tree:
    print(item)

The tree() method returns a list of strings representing the directory tree structure.

Conclusion

The droot package provides a simple and intuitive way to manage files and directories in Python. It offers a range of operations for creating, deleting, copying, moving, and iterating over files and directories. For more details, refer to the package documentation.

Metadata

Release files for droot 1.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 droot 1.0.2
File Size Uploaded
droot-1.0.2.tar.gz 3.5 kB Details

Release files / droot-1.0.2.tar.gz

Download URL droot-1.0.2.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9b527a4d9b0b306b4eba96c1073315a0b911ab77d519a88ca5b34eae3bb6ad46
BLAKE2b-256 checksum
How to use checksums
3a30c420e51d160c4fce0efd7e090a36f02930528d76e2fce7a3c0e4cd12ce5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release history Release notifications | RSS feed

This release

1.0.2 This release

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