Skip to content

philip928lin/PathNavigator

Repository files navigation

PyPI Docs Test

PathNavigator

PathNavigator is a Python package designed to navigate directories and files efficiently. It provides tools to interact with the filesystem, allowing users to create, delete, and navigate folders and files, while also maintaining an internal representation of the directory structure. Customized shortcuts can be added. The paths are stored as Path objects from pathlib, which adapt automatically across platforms.

Installation

pip install PathNavigator

Install the latest version from GitHub repo:

pip install git+https://github.com/philip928lin/PathNavigator.git

Get start

from pathnavigator import PathNavigator

pn = PathNavigator("root_dir")

# Now you are able to access all subfolders and files under `root_dir`
dir_to_your_subfolder = pn.your_subfolder.get()
path_to_your_file = pn.your_subfolder.get("your_file.csv")  # return the full path to your_file.csv.

# Convert a Path object to a string
path_string = str(dir_to_your_subfolder)

# Prints a visual tree structure of the folder and its contents.
pn.tree() 
pn.your_subfolder.tree()

Features

Creating Directories

pn = PathNavigator('/path/to/root')
pn.mkdir('folder1')     # make a subfolder under the root.
pn.folder1.mkdir('folder2')     # make a subfolder under folder1.

Reloading folder structure

# Update the folder structure to fit the latest structure in the file system.
pn.reload() 

System Path Management

# Add the directory to folder1 to sys path.
pn.forlder1.add_to_sys_path()   

Changing Directories

# Change the working directory to folder2.
pn.forlder1.forlder2.chdir()    

Directory and File Operations

# Returns the full path to folder1.
pn.folder1.get()       

# Return the full path to file1.
pn.folder1.get("file.csv")  

# Rrints the contents (subfolders and files) of folder1.
pn.folder1.ls()         

# Make the nested directories.
pn.folder1.mkdir("subfolder1", "subsubfolder2")

# Removes a file or subfolder from the folder and deletes it from the filesystem.
pn.folder1.remove('folder2')    

# Combine folder1 directory with "subfolder1/fileX.txt" and return it.
pn.folder1.join("subfolder1", "fileX.txt") 

# Or, you can utilize Path feature to join the paths.
pn.folder1.get() / "subfolder1/fileX.txt"

Shortcuts Management

Add shortcuts

# Set a shortcut named "f1" to folder1.
pn.folder1.set_shortcuts("f1")

# Set a shortcut named "x" to the file "x.txt" in folder1.
pn.folder1.set_shortcuts("x", "x.txt")

# Directly add shortcuts in pn.sc
pn.sc.add('f', pn.folder1.get("file"))  
pn.sc.add('f', r"new/path")  

Retrieve shortcuts

# Retrieve the path of "f1" shortcut
pn.sc.get("f1")  
# Or, access it using "valid" attribute names.
pn.sc.f1    

Other shortcut operations

# Print all shortcuts
pn.sc.ls()       

# Remove a shortcut
pn.sc.remove('f')   

# Return a dictionary of shortcuts
pn.sc.to_dict()  

# Output of shortcuts json file
pn.sc.to_json(filename)  

# Load shortcuts from a dictionary
pn.sc.load_dict()  

# Load shortcuts from a json file
pn.sc.load_json(filename)  

API reference

Docs

About

A Python package designed to navigate directories and files efficiently.

Resources

License

Stars

Watchers

Forks

Packages

No packages published