FileSystem is a powerful toolkit designed to handle file and directory operations with ease and efficiency across various operating systems.
Project description
FileSystem Pro
FileSystem is a powerful toolkit designed to handle file and directory operations with ease and efficiency across various operating systems.
Getting Started
Recommendation
It's recommended Python 3.8 or later to use FileSystem Pro. You can download the latest version of Python in python.org.
Installation
Don't forget to upgrade pip:
pip install --upgrade pip
And install FileSystem Pro:
pip install filesystempro
To Developers / Contributors
Clone this repository to your local machine using:
git clone https://github.com/hbisneto/FileSystemPro.git
Install setuptools
pip install setuptools
Upgrade setuptools
pip install --upgrade setuptools
[!NOTE] Note: FileSystem Pro requires setuptools 69.5.1 or later.
Python environment typically targets setuptools version 49.x.
Install wheel
pip install wheel
Upgrade wheel
pip install --upgrade wheel
Features
- Cross-platform Compatibility: The code is designed to work on multiple operating systems, including Linux, Mac, and Windows. This makes it versatile and adaptable to different environments.
- Directory Path Identification: The code identifies and defines the paths to several common user directories based on the operating system. This includes directories like Desktop, Documents, Downloads, Music, Pictures, Public, Videos, and others.
- Current Working Directory: The code uses
os.getcwd()
to get the current working directory. - String Formatting: The code uses f-string formatting to create directory paths.
- Monitoring System: Watcher acts as a monitoring system for the file system. It keeps track of all activities within the file system.
- Change Tracking: It records any changes made within the file system. This includes the creation of new files, modification of existing files, and deletion of files.
- Real-Time Updates: The Watcher provides real-time updates on any changes made within the file system. This ensures that users have the most current information at all times.
- Integrity Maintenance: This feature is particularly useful in scenarios where maintaining the integrity and up-to-date status of the file system is crucial. By tracking all changes, the Watcher helps ensure that the file system remains accurate and reliable.
FileSystem
import filesystem as fs
Method | Description |
---|---|
CURRENT_LOCATION | Creates a string that represents the path to the current directory. (Where the application is running) |
OS_SEPARATOR |
prints the OS separator
'/' for macOS and Linux '\\' for Windows |
USER_NAME | Creates a string that represents the username of the user currently logged in to the system. |
user | Creates a string that represents the path to the current user's home directory. |
desktop | Creates a string that represents the path to the current user's Desktop folder. |
documents | Creates a string that represents the path to the current user's Documents folder. |
downloads | Creates a string that represents the path to the current user's Downloads folder. |
music | Creates a string that represents the path to the current user's Music folder. |
pictures | Creates a string that represents the path to the current user's Pictures folder. |
public | Creates a string that represents the path to the current user's Public folder. |
videos | Creates a string that represents the path to the current user's Videos folder. |
linux_templates | Creates a string that represents the path to the current user's Templates folder in Linux environment. |
mac_applications | Creates a string that represents the path to the current user's Applications folder in macOS environment. |
windows_applicationData | Creates a string that represents the path to the current user's Roaming folder inside AppData in Windows environment. |
windows_favorites | Creates a string that represents the path to the current user's Favorites folder in Windows environment. |
windows_localappdata | Creates a string that represents the path to the current user's Local folder inside AppData in Windows environment. |
windows_temp | Creates a string that represents the path to the current user's Temp folder inside LocalAppData in Windows environment. |
Sample Codes
FileSystem: Reaching Desktop Folder
The following example shows how to get the Desktop
directory path
import filesystem as fs
desk = fs.desktop
print(desk)
Output:
## On Linux
/home/YOU/Desktop
## On macOS
/Users/YOU/Desktop
## On Windows
C:\Users\YOU\Desktop
Console
from filesystem import console as fsconsole
Console is a robust library designed to enable ANSI escape character sequences, which are used for generating colored terminal text and cursor positioning. This library is a key addition to FileSystemPro as a third-party library, enhancing the toolkit for developers who require consistent terminal styling across different operating systems.
Features
- Universal Compatibility: Console ensures that applications or libraries utilizing ANSI sequences for colored output on Unix or Macs can now operate identically on Windows systems.
- Simplified Integration: With no dependencies other than the standard library, integrating Console into your projects is straightforward. It’s tested across multiple Python versions, ensuring reliability.
- Enhanced Terminal Experience: By converting ANSI sequences into appropriate win32 calls, Console allows Windows terminals to emulate the behavior of Unix terminals, providing a consistent user experience.
- Effortless Transition: For developers transitioning to FileSystemPro, incorporating Console into your workflow is effortless, enabling you to maintain the visual aspects of your terminal applications without platform constraints.
Constants | Colors |
---|---|
foreground | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. |
background | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. |
style | DIM, NORMAL, BRIGHT, RESET_ALL |
Sample Codes
[!NOTE] Please note that GitHub (and PYPI) does not support colored text in README files. This is due to the limitations of the markdown language used in GitHub (and PYPI) READMEs, which does not have built-in support for text color changes.
Console: Printing a red foreground text message
The following example shows how to print some red foreground texts using Console
from filesystem import console as fsconsole
# This will print a spaced text to your print message
print(fsconsole.foreground.RED, "This is a warn message")
# This will print a no space text to your print message
print(fsconsole.foreground.RED + "This is another warn message")
# You can use f-string format to assign the color to your print
print(f'{fsconsole.foreground.RED}This is a new warn message{fsconsole.foreground.RESET}')
# This text will be printed without color (default)
print("This is a normal text")
Output:
This is a warn message
This is another warn message
This is a new warn message
This is a normal text
Console: Printing a blue background text message
The following example shows how to print some blue background texts using Console
from filesystem import console as fsconsole
# This will print a spaced text to your print message
print(fsconsole.background.BLUE, 'This is a blue background message')
# This will print a no space text to your print message
print(fsconsole.background.BLUE + 'This is another blue background message')
# You can use f-string format to assign the color to your print
print(f'{fsconsole.background.BLUE}This is a new blue background message{fsconsole.background.RESET}')
# This text will be printed without color (default)
print('This is a normal text')
Output:
This is a blue background message
This is another blue background warn message
This is a new blue background message
This is a normal text
Console: Different foregrounds, backgrounds and styles
The following example shows how to print some texts with different backgrounds, foregrounds and styles using Console
# Prints a red foreground text
print(f'{fsconsole.foreground.RED}Some red text')
# Prints a red foreground text with a green background
print(f'{fsconsole.background.GREEN}And with a green background{fsconsole.style.RESET_ALL}')
# Prints a dim normal text with no background
print(f'{fsconsole.style.DIM}And in dim text{fsconsole.style.RESET_ALL}')
# Prints a normal text
print('Back to normal color')
Output:
Some red text
And with a green background
And in dim text
Back to normal color
Remember, for the color changes to work, your Terminal must support ANSI escape sequences, which are used to set the color. Not all Terminals do, so if you’re not seeing the colors as expected, that could be why.
Directory
from filesystem import directory as dir
The Directory module is a component of the FileSystemPro library that provides a collection of functions for handling directory-related operations. It simplifies tasks such as path manipulation, directory creation and deletion, and file retrieval within directories.
Features
- Path Combination: Dynamically combines multiple paths into a single path string.
- Directory Creation: Creates new directories, with an option to create necessary subdirectories.
- Directory Deletion: Deletes directories, with an option for recursive deletion.
- Directory Existence Check: Checks whether a directory exists at a specified path.
- File Retrieval: Retrieves a list of files within a directory using glob patterns.
- Parent Directory Information: Retrieves the name or path of a file's parent directory.
- Directory Listing: Lists all subdirectories within a given directory.
- Directory Renaming: Renames a directory if it exists.
Method | Description |
---|---|
directory.combine(*args, paths=[]) | Combines a list of paths or arguments into a single path. If the first argument or the first element in the paths list is not an absolute path, it raises a ValueError. |
directory.create(path, create_subdirs = True) | Creates a directory at the specified path. If `create_subdirs` is True, all intermediate-level directories needed to contain the leaf directory will be created. After the directory is created, it returns the details of the created directory. |
directory.delete(path, recursive=False) | Deletes a directory at the specified path. If `recursive` is True, the directory and all its contents will be removed. |
directory.exists(path) | Checks if a directory exists at the specified path. |
directory.get_parent_name(path) | Retrieves the parent directory name from the specified path. |
directory.get_parent(path) | Retrieves the parent directory from the specified path. |
directory.get_name(path) | Retrieves the name of the directory of the specified path. If the path has an extension, it is assumed to be a file, and the parent directory name is returned. If the path does not have an extension, it is assumed to be a directory, and the directory name is returned. |
directory.join(path1='', path2='', path3='', path4='', paths=[]) | Joins multiple directory paths into a single path. The function ensures that each directory path ends with a separator before joining. If a directory path does not end with a separator, one is added. |
directory.get_directories(path) | Lists all directories in the specified path. |
directory.rename(old_path, new_path) | Renames a directory from the old directory path to the new directory path. If the old directory path does not exist or is not a directory, the function returns False. |
Sample Codes
Directory: Check if exists
The following example check whether a directory exists within the file system and print the result using Directory
import filesystem as fs
from filesystem import directory as dir
documents_exists = dir.exists(fs.documents)
print(documents_exists)
Output:
True
Directory: Listing directories inside a folder
The following example shows how to lists all directories in the specified path using Directory
import filesystem as fs
from filesystem import directory as dir
folder_list = dir.get_directories(fs.documents)
print(folder_list)
Output:
['Work', 'School', 'PicsBackups', 'Office Documents']
Directory: Renaming a folder
The following example shows how rename a folder using Directory
import filesystem as fs
from filesystem import directory as dir
new_name = dir.rename(f'{fs.documents}/MyFolder', f'{fs.documents}/NewFolder')
print(new_name)
Output:
True
File
from filesystem import file as fsfile
The File module is a comprehensive utility toolset that forms part of the FileSystemPro library. It provides a suite of functions designed to handle various file operations such as integrity checks, file creation, deletion, enumeration, and file splitting and reassembling.
Features
- Checksum Calculation: Utilizes SHA-256 hashing to calculate file checksums for integrity verification.
- Integrity Check: Compares checksums of two files to verify their integrity.
- File Creation: Supports creating both text and binary files with specified data.
- File Deletion: Safely deletes files by checking for their existence before removal.
- File Enumeration: Enumerates all files in a given directory, providing detailed file information.
- File Existence Check: Determines if a file exists at a given path.
- File Listing: Lists all files in a specified directory.
- File Renaming: Renames files within a directory after checking for their existence.
- File Reassembling: Reassembles split files back into a single file.
- File Splitting: Splits a file into smaller parts based on a specified chunk size.
Method | Description |
---|---|
file.calculate_checksum(file) | Calculates the SHA-256 checksum of a file. This function reads the file in binary mode and updates the hash in chunks to efficiently handle large files. |
file.check_integrity(file, reference_file) | Compares the SHA-256 checksums of two files to verify their integrity. This function is useful for ensuring that a file has not been altered or corrupted by comparing it to a reference file. |
file.create(file, data, encoding="utf-8-sig") | Creates a file at the specified path and writes data into it. If the file already exists, its contents are overwritten. The function then returns the details of the created file. |
file.create_binary_file(filename, data) | Creates a binary file at the specified filename and writes data into it. If the data is not of bytes type, it is first encoded to bytes. |
file.delete(file) | Deletes a file at the specified path if it exists. |
file.enumerate_files(file) | Enumerates all files in a given directory and its subdirectories. For each file and directory, it retrieves various attributes using the `wra.get_object` function. |
file.exists(file) | Checks if a file exists at the specified path. |
file.get_files(path) | Retrieves all files in a given directory. |
file.move(source, destination, new_filename=None, replace_existing=False) | The move function moves a file from a source location to a destination location. If the destination file already exists, you can choose whether to replace it or keep the existing file. |
file.rename(old_name, new_name) | Renames a file in a given directory from `old_name` to `new_name`. |
file.reassemble_file(large_file, new_file) | Reassembles a file that was previously split into parts. The function checks for the existence of the split parts and reads each part, writing it to a new file. After all parts have been written to the new file, the function deletes the parts. |
file.split_file(file, chunk_size = 1048576) | Splits a large file into smaller chunks. The function reads the file in chunks of a specified size and writes each chunk to a new file. The new files are named by appending `.fsp` and an index number to the original filename. |
Sample Codes
File: Check Integrity
The following example check the integrity of a file against a reference file and print the result using File
from filesystem import file as fsfile
integrity = fsfile.check_integrity("/path/to/file", "/path/to/reference_file")
print("Files are identical:", integrity)
Output:
Files are identical: True
File: Split File
The following example shows how to split a large file into 500 KB chunks using File
from filesystem import file as fsfile
is_split = fsfile.split_file("large_file.iso", 512000)
print(is_split)
Output:
True
File: Reassemble File
The following example shows how to reassemble a file that was previously split into parts using File
from filesystem import file as fsfile
fsfile.reassemble_file("large_file.iso", "new_file.iso")
Output:
None output
Wrapper
from filesystem import wrapper as wra
Wrapper is a comprehensive toolkit that provides a set of utility functions specifically designed to facilitate file and directory operations. These operations may include creating, reading, updating, and deleting files or directories.
Method | Description | Status |
---|---|---|
combine(*args, paths=[]) |
This function is designed to combine file or directory paths. It takes any number of arguments *args and an optional parameter paths which is a list of paths. The function returns a combined path based on the inputs.
If the paths list is provided, the function uses it to combine paths. It starts with the first path in the list and checks if it’s an absolute path. If it’s not, it raises a ValueError with a detailed error message. Then, it iterates over the rest of the paths in the list. If a path is absolute, it replaces the current result with this path. If a path is relative, it joins this path to the current result. Finally, it returns the combined path.
If the paths list is not provided or is empty, the function uses the arguments passed *args. It starts with the first argument and checks if it’s an absolute path. If it’s not, it raises a ValueError with a detailed error message. Then, it iterates over the rest of the arguments. If an argument is an absolute path, it replaces the current result with this path. If an argument is a relative path and not an empty string, it adds this path to the current result. If the current result doesn’t end with a separator (os.sep), it adds one before adding the path. Finally, it returns the combined path.
Please note: This function does not check if the paths exist or are valid, it only combines them based on the rules described. It’s up to the caller to ensure that the paths are valid and exist if necessary. This method is intended to concatenate individual strings into a single string that represents a file path. However, if an argument other than the first contains a rooted path, any previous path components are ignored, and the returned string begins with that rooted path component. As an alternative to the combine method, consider using the join method. |
Under support. Consider using directory.combine(*args, paths=[]) |
create_directory(path, create_subdirs=True) |
This function is used to create a directory at the specified path. If create_subdirs is True, the function creates all intermediate-level directories needed to contain the leaf directory. If create_subdirs is False, the function will raise an error if the directory already exists or if any intermediate-level directories in the path do not exist.
Default is True If the directories already exist, it does nothing. |
Under support. Consider using directory.create(path, create_subdirs = True) |
create_file(file_name, path, text, encoding="utf-8-sig") |
The function attempts to open a file at the specified path with the given file_name (with extension), in write mode with the specified encoding. It then writes the provided text into the file.
Finally, it calls Wrapper get_object with the full path to the newly created file and returns the resulting dictionary. |
Under support. Consider using file.create(file, data, encoding="utf-8-sig") |
delete(path, recursive=False) | This function is designed to delete a directory at a given path. If recursive is set to True, the function will delete the directory and all its contents. If it’s False, the function will only delete the directory if it’s empty. Default is False. | Under support. Consider using directory.delete(path, recursive=False) |
find_duplicates(path) | Finds duplicate files in a given directory and its subdirectories. A file is considered a duplicate if it has the same checksum as another file. | New implementation |
enumerate_files(path) | This function performs a depth-first traversal of the directory tree at the given path (after expanding any user home directory symbols). It returns a list of dictionaries containing the attributes of each file and directory in the tree. | Under support. Consider using file.enumerate_files(path) |
get_files(path) | This function takes a path as input (which can include wildcards), expands any user home directory symbols (~), and returns a list of dictionaries containing the attributes of each file or directory that matches the path. | Under support. Consider using file.get_files(path) |
get_object(path) | This function takes a file or directory path as input and returns a dictionary containing various attributes of the file or directory. These attributes include the time of last modification, creation time, last access time, name, size, absolute path, parent directory, whether it's a directory or file or link, whether it exists, and its extension (if it's a file). | Supported |
get_size(file_path) | Calculates the size of the file or directory at the specified path. If the path is a directory, it calculates the total size of all files in the directory. The size is returned in bytes, KB, MB, GB, or TB, depending on the size. | New implementation |
has_extension(file_path) | Checks if the given file path has an extension. This function can return True or False based on the string, even if the file or directory does not exist. | New implementation |
join(path1='', path2='', path3='', path4='', paths=[]) |
This function is designed to concatenate directory paths. It takes four optional string parameters path1, path2, path3, path4 and an optional list of paths paths. The function returns a single string that represents the concatenated path.
For each of the parameters path1, path2, path3 and path4, the function checks if the path ends with a separator. If it doesn’t, and the path is not an empty string, it adds a separator to the end of the path. If the paths list is provided and is not empty, the function iterates over each item in the list. For each item, it checks if the item ends with a separator. If it doesn’t, it adds a separator to the end of the item. Finally, the function returns the concatenated path. Please note: This function does not check if the paths exist or are valid, it only combines them based on the rules described. It’s up to the caller to ensure that the paths are valid and exist if necessary. Unlike the combine method, the join method does not attempt to root the returned path. (That is, if path2 or path3 or path4 is an absolute path, the join method does not discard the previous paths as the combine method does). |
Under support. Consider using directory.join(path1='', path2='', path3='', path4='', paths=[]) |
list_directories(path) | This function returns a list of all the directories in a given directory. | Under support. Consider using directory.get_directories(path) |
list_files(path) | This function returns a list of all the files in a given directory. | Under support. Consider using file.get_files(path) |
make_zip(source, destination) | This function is used to create a zip archive of a given source directory and move it to a specified destination. | Supported |
Sample Codes
Wrapper: Make a zip file
Creates a zip archive of the specified source directory or file and moves it to the specified destination using Wrapper
- Creates a zip archive of a directory and moves it to a destination.
from filesystem import wrapper as wra
wra.make_zip("/path/to/directory", "/path/to/directory.zip")
- Creates a zip archive of a file and moves it to a destination.
from filesystem import wrapper as wra
wra.make_zip("/path/to/file.txt", "/path/to/file.zip")
Wrapper: Has Extension
Checks if the given file path has an extension
from filesystem import wrapper as wra
bool_extension = wra.has_extension("/path/to/file.txt")
print(bool_extension)
Output
True
This will return True because the file has an extension (.txt).
Checks if the given file path has an extension
from filesystem import wrapper as wra
bool_extension = wra.has_extension("/path/to/file")
print(bool_extension)
Output
False
This will return False because the file does not have an extension.
Wrapper: Get size of file or directory
Calculates the size of the file or directory at the specified path.
import filesystem as fs
from filesystem import wrapper as wra
documents_size = wra.get_size(fs.documents)
print(documents_size)
Output:
1.6 GB
Watcher
from filesystem import watcher as wat
Watcher serves as a monitoring system for the file system. It keeps track of any changes made within the file system, such as the creation of new files, modification of existing files, or deletion of files. This feature allows for real-time updates and can be particularly useful in scenarios where maintaining the integrity and up-to-date status of the file system is crucial.
Method | Description |
---|---|
init(self, root) | This is the constructor method that initializes the Watcher object with a root directory to watch. It also saves the current state of the file system in self.saved_state. |
get_state(self, path): | This method returns a dictionary where the keys are the absolute paths of all files in the given path and the values are file metadata obtained from the wrapper.enumerate_files(path) function. |
diff(self) | This method compares the current state of the file system with the saved state and identifies any changes (created, updated, or removed files). It returns a list of dictionaries where each dictionary contains the metadata of a changed file and an additional key "change" indicating the type of change. |
str(self) | This method returns a string representation of the Watcher object. |
This class could be useful in scenarios where you need to monitor changes to a file system, for example, in a backup system or a live syncing service.
Sample Codes
Watcher: Monitoring Documents Folder
This Watcher example is designed to monitor changes in Documents directory and print out the changes as they occur.
# Native library
import time
from datetime import datetime
# FileSystemPro
import filesystem as fs
from filesystem import watcher as wat
# Create a new instance of Watcher class
watcher = wat.Watcher(f'{fs.documents}')
# Run `diff` method to get directory changes
while True:
changes = watcher.diff()
if changes:
print(f"Changes detected at: {datetime.now()}:")
for change in changes:
print(f"{change['abspath']} was {change['change']}")
time.sleep(5) # Awaits for 5 seconds before a new verification
Copyright © 2023–2024 Bisneto Inc. All rights reserved.
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
Hashes for filesystempro-1.0.3.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ad0568302e6be2a183039216ddaa8f083227b94cac6360d03182dfcb84894c9 |
|
MD5 | 978ad90433402eef8e60b294130ad224 |
|
BLAKE2b-256 | e84f3f5cee2945574df9c1f2eb851f672f3976f0895184c8f5a47759c9066a84 |