Socket
Socket
Sign inDemoInstall

filesystempro

Package Overview
Dependencies
1
Maintainers
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    filesystempro

FileSystem Pro is designed to identify the operating system (OS) on which it`s running and define the paths to various user directories based on the OS.


Maintainers
1

Readme

FileSystem Pro

FileSystem Pro is designed to identify the operating system (OS) on which it’s running and define the paths to various user directories based on the OS.

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: 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
MethodDescription
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

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.

MethodDescription
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.
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.
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.
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.
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.
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.
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).
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).
list_directories(path) This function returns a list of all the directories in a given directory.
list_files(path) This function returns a list of all the files in a given directory.
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.

Sample Codes

Wrapper: Creating a Folder

The following example shows how to create a new directory named database inside the Documents directory using Wrapper

import filesystem as fs
from filesystem import wrapper as wra

bd_folder = "database"
try:
   wra.create_directory(f'{fs.documents}/{bd_folder}')
except:
   print("Could`t create the folder")
Wrapper: Get Files
  1. Get Files:

The following example shows how to get files information from Downloads folder.

import filesystem as fs
from filesystem import wrapper as wra

pointers = wra.get_files(f'{fs.downloads}/*')
print(pointers)

Output:

[{'modified': 1695535334.1411633, 'created': 1697604128.7045012, 'access': 1697604129.781534, 'name': 'CLI.py', 'size': 3345, 'abspath': '/Users/YOU/Downloads/CLI.py', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'py'}, {'modified': 1697605101.6574, 'created': 1697683292.4821024, 'access': 1697683294.46923, 'name': 'Python_Logo.png', 'size': 747809, 'abspath': '/Users/YOU/Downloads/Python_Logo.png', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'png'}, {'modified': 1697681746.0940206, 'created': 1697682027.268841, 'access': 1697682292.5433743, 'name': 'Sample_File.py', 'size': 1031, 'abspath': '/Users/YOU/Downloads/Sample_File.py', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'py'}]

  1. Filter Files by Extension:

The following example is using a list comprehension to filter out files with extension .py from the pointers list:

py_files = [x for x in pointers if x["extension"] == "py"]
print(py_files)
[{'modified': 1695535334.1411633, 'created': 1697604128.7045012, 'access': 1697604129.781534, 'name': 'CLI.py', 'size': 3345, 'abspath': '/Users/YOU/Downloads/CLI.py', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'py'}, {'modified': 1697681746.0940206, 'created': 1697682027.268841, 'access': 1697681829.0075543, 'name': 'Sample_File.py', 'size': 1031, 'abspath': '/Users/YOU/Downloads/Sample_File.py', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'py'}]

  1. Get File Names Inside the Filter: The following code is using a list comprehension that prints the names of all filtered files in the py_files list:
print([x["name"] for x in py_files])

Output:

['CLI.py', 'Sample_File.py']
Wrapper: Enumerate files (walk recursively) from a directory

The following code is using a list comprehension to generate a list of all files in the Downloads directory:

tree = [x for x in wra.enumerate_files(fs.downloads)]
print(tree)

Output:

[{'modified': 1697683292.4821026, 'created': 1697683292.4821026, 'access': 1697683292.484029, 'name': 'Downloads', 'size': 224, 'abspath': '/Users/YOU/Downloads', 'dirname': '/Users/YOU', 'is_dir': True, 'is_file': False, 'is_link': False, 'exists': True, 'extension': ''}, {'modified': 1697683288.8639557, 'created': 1697683288.8639557, 'access': 1697602943.1846778, 'name': '.DS_Store', 'size': 6148, 'abspath': '/Users/YOU/Downloads/.DS_Store', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'DS_Store'}, {'modified': 1690685751.342114, 'created': 1690685751.4194765, 'access': 1690685751.342114, 'name': '.localized', 'size': 0, 'abspath': '/Users/YOU/Downloads/.localized', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'localized'}, {'modified': 1695535334.1411633, 'created': 1697604128.7045012, 'access': 1697604129.781534, 'name': 'CLI.py', 'size': 3345, 'abspath': '/Users/YOU/Downloads/CLI.py', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'py'}, {'modified': 1697605101.6574, 'created': 1697683292.4821024, 'access': 1697683294.46923, 'name': 'Python_Logo.png', 'size': 747809, 'abspath': '/Users/YOU/Downloads/Python_Logo.png', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'png'}, {'modified': 1697681746.0940206, 'created': 1697682027.268841, 'access': 1697682292.5433743, 'name': 'Sample_File.py', 'size': 1031, 'abspath': '/Users/YOU/Downloads/Sample_File.py', 'dirname': '/Users/YOU/Downloads', 'is_dir': False, 'is_file': True, 'is_link': False, 'exists': True, 'extension': 'py'}]

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.

MethodDescription
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

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.
ConstantsColors
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.


Copyright © 2023–2024 Bisneto Inc. All rights reserved.

Keywords

FAQs


Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc