FileSystem Module
The FileSystem module provides a comprehensive suite of methods for managing and interacting with various directories and user folders across multiple operating systems. It intelligently identifies the user’s operating system—Linux, macOS, or Windows—and configures file paths for essential directories like Desktop, Documents, Downloads, Music, Pictures, Public, and Videos. Leveraging Python’s built-in libraries such as os, sys, and getpass, it ensures cross-platform compatibility and accurate path retrieval. For Windows environments, it uses the winreg module to query the Windows registry, ensuring the paths to these directories are accurately retrieved based on the system’s registry settings.
Features
Cross-platform Compatibility: Works on Linux, macOS, and Windows, making it versatile and adaptable to different environments.
Directory Path Identification: Identifies and defines paths to common user directories such as Desktop, Documents, Downloads, Music, Pictures, Public, and Videos.
String Formatting: Uses f-string formatting to create directory paths.
Registry-Based Path Retrieval: On Windows, uses the winreg module to retrieve accurate paths from the Windows registry for folders like Home, Desktop, Documents, Downloads, Music, Pictures, Public, and Videos.
Methods
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-specific 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 a Linux environment. |
mac_applications |
Creates a string that represents the path to the current user’s Applications folder in a macOS environment. |
windows_applicationData |
Creates a string that represents the path to the current user’s Roaming folder inside AppData in a Windows environment. |
windows_favorites |
Creates a string that represents the path to the current user’s Favorites folder in a Windows environment. |
windows_localappdata |
Creates a string that represents the path to the current user’s Local folder inside AppData in a Windows environment. |
windows_temp |
Creates a string that represents the path to the current user’s Temp folder inside LocalAppData in a Windows environment. |
The paths for Windows environments are retrieved using the winreg module to query the Windows registry for accurate and current folder locations. This ensures that paths such as Desktop, Documents, Downloads, Music, Pictures, Public, and Videos are accurately defined for the user based on the system’s registry settings.
API Reference
# FileSystem
—
## Overview FileSystem is a library for managing file paths in a user’s system, depending on the operating system (OS) they are using. It uses Python’s built-in libraries like os, sys, and getpass to interact with the system and manage file paths.
Here’s a brief description of what the code does:
1. User Identification: It identifies the current user using the getpass.getuser() function and stores the username with the first letter capitalized.
2. Platform Identification: It identifies the platform (OS) using sys.platform. Depending on whether the platform is Linux, macOS, or Windows, it sets up different file paths.
3. File Paths: For each platform, it sets up file paths for common user directories like Desktop, Documents, Downloads, Music, Pictures, Public, and Videos. The paths are formed using the user’s home directory path and the standard directory names for each platform.
For Linux, it uses the /home/{username} directory as the base.
For macOS, it uses the /Users/{username} directory as the base.
For Windows, it uses the USERPROFILE environment variable to get the base directory.
4. Special Directories: Apart from the common directories, it also sets up paths for some special directories based on the platform. For example, Templates in Linux, Applications and Movies in macOS, and several AppData related paths in Windows.
This library can be useful for scripts that need to work with user files and need to be compatible across different operating systems. It provides an easy way to get the correct file paths regardless of the platform.
- filesystem.CURRENT_LOCATION = '/home/docs/checkouts/readthedocs.org/user_builds/filesystempro/checkouts/latest/docs'
Creates a string that represents the path to the current directory. (Where the application is running)
- filesystem.OS_SEPARATOR = '/'
The os.sep is an attribute in the os module in Python. It represents the character that is used by the operating system to separate pathname components, and it varies between different operating systems.
For instance, on Windows, it would return a backslash (), while on Unix or Linux, it would return a forward slash (/). So, OS_SEPARATOR will contain the appropriate file path separator for the operating system on which the Python script is running. This is useful for creating file paths in a cross-platform compatible way.
- filesystem.USER_NAME = 'Docs'
Creates a string that represents the username of the user currently logged in to the system.
- filesystem.desktop = '/home/docs/Desktop'
Creates a string that represents the path to the current user’s Desktop folder.
- filesystem.documents = '/home/docs/Documents'
Creates a string that represents the path to the current user’s Documents folder.
- filesystem.downloads = '/home/docs/Downloads'
Creates a string that represents the path to the current user’s Downloads folder.
- filesystem.linux_templates = '/home/docs/Templates'
Creates a string that represents the path to the current user’s Templates folder in Linux environment.
- filesystem.mac_applications = '/home/docs/Applications'
Creates a string that represents the path to the current user’s Applications folder in macOS environment.
- filesystem.mac_movies = '/home/docs/Movies'
Creates a string that represents the path to the current user’s Movies folder in macOS environment.
Tip: Use fs.videos instead:
`python import filesystem as fs print(fs.videos) `
- filesystem.music = '/home/docs/Music'
Creates a string that represents the path to the current user’s Music folder.
- filesystem.pictures = '/home/docs/Pictures'
Creates a string that represents the path to the current user’s Pictures folder.
- filesystem.public = '/home/docs/Public'
Creates a string that represents the path to the current user’s Public folder.
- filesystem.user = '/home/docs'
Creates a string that represents the path to the current user’s home directory.
- filesystem.videos = '/home/docs/Videos'
Creates a string that represents the path to the current user’s Videos folder.
- filesystem.windows_applicationData = '/home/docs/AppData/Roaming'
Creates a string that represents the path to the current user’s Roaming folder inside AppData in Windows environment.
- filesystem.windows_favorites = '/home/docs/Favorites'
Creates a string that represents the path to the current user’s Favorites folder in Windows environment.
- filesystem.windows_localappdata = '/home/docs/AppData/Local'
Creates a string that represents the path to the current user’s Local folder inside AppData in Windows environment.
- filesystem.windows_temp = '/home/docs/AppData/Local/Temp'
Creates a string that represents the path to the current user’s Temp folder inside LocalAppData in Windows environment.
Example: Reaching the Desktop Folder
import filesystem as fs
desk = fs.desktop
print(desk)
Output (example paths):
On Linux:
/home/YOU/DesktopOn macOS:
/Users/YOU/DesktopOn Windows:
C:\\Users\\YOU\\Desktop