Few File Utilities and some OS Functions
Project description
File Utilities
Few File Utilities and some OS Functions
Table of Contents
Install
pip install ddcUtils
Conf File Utils
File example - file.ini:
[main]
files=5
path="/tmp/test_dir"
port=5432
list=1,2,3,4,5,6
GET_ALL_VALUES
Get all values from an .ini config file structure and return them as a dictionary.
The mixed_values parameter will return all values as an object instead of dict.
from ddcUtils import ConfFileUtils
cfu = ConfFileUtils()
# Get all values as dictionary
all_values = cfu.get_all_values("/path/to/config.ini", mixed_values=False)
print(all_values) # {'main': {'files': '5', 'path': '/tmp/test_dir', 'port': '5432', 'list': '1,2,3,4,5,6'}}
# Get all values as object
all_values_obj = cfu.get_all_values("/path/to/config.ini", mixed_values=True)
print(all_values_obj.main.files) # 5
GET_SECTION_VALUES
Get all section values from an .ini config file structure and return them as a dictionary.
from ddcUtils import ConfFileUtils
cfu = ConfFileUtils()
# Get all values from 'main' section
section_values = cfu.get_section_values("/path/to/config.ini", "main")
print(section_values) # {'files': '5', 'path': '/tmp/test_dir', 'port': '5432', 'list': '1,2,3,4,5,6'}
GET_VALUE
Get a specific value from an .ini config file structure and return it.
from ddcUtils import ConfFileUtils
cfu = ConfFileUtils()
# Get specific value from config
port = cfu.get_value("/path/to/config.ini", "main", "port")
print(port) # "5432"
path = cfu.get_value("/path/to/config.ini", "main", "path")
print(path) # "/tmp/test_dir"
SET_VALUE
Set a value in an .ini config file structure and return True for success or False for failure.
from ddcUtils import ConfFileUtils
cfu = ConfFileUtils()
# Set a new value in config file
success = cfu.set_value("/path/to/config.ini", "main", "port", "8080")
print(success) # True
# Set a list value with comma separation
success = cfu.set_value("/path/to/config.ini", "main", "servers", "server1,server2,server3", commas=True)
print(success) # True
File Utils
OPEN
Open the given file or directory in the system's default application (explorer/file manager or text editor) and returns True for success or False for failed access.
from ddcUtils import FileUtils
fu = FileUtils()
# Open file in default application
success = fu.open("/path/to/document.txt")
print(success) # True
# Open directory in file explorer
success = fu.open("/path/to/directory")
print(success) # True
LIST_FILES
List all files in the given directory and return them in a tuple sorted by creation time in ascending order.
Supports filtering by file prefix and suffix.
from ddcUtils import FileUtils
fu = FileUtils()
# List all files in directory
all_files = fu.list_files("/home/user/documents")
print(all_files) # ('file1.txt', 'file2.pdf', 'image.jpg')
# List files starting with 'test'
test_files = fu.list_files("/home/user/documents", starts_with="test")
print(test_files) # ('test_data.csv', 'test_results.txt')
# List Python files
py_files = fu.list_files("/home/user/projects", ends_with=".py")
print(py_files) # ('main.py', 'utils.py', 'config.py')
GZIP
Compress the given file using gzip compression and returns the Path object for success or None if the operation failed.
from ddcUtils import FileUtils
fu = FileUtils()
# Compress file to the same directory
compressed_path = fu.gzip("/path/to/large_file.txt")
print(compressed_path) # /path/to/large_file.txt.gz
# Compress file to specific directory
compressed_path = fu.gzip("/path/to/large_file.txt", output_dir="/path/to/compressed")
print(compressed_path) # /path/to/compressed/large_file.txt.gz
UNZIP
Extracts the contents of a ZIP file to the specified output directory and returns ZipFile object for success or None if the operation failed.
from ddcUtils import FileUtils
fu = FileUtils()
# Extract to the same directory as ZIP file
zip_file = fu.unzip("/path/to/archive.zip")
print(zip_file) # <zipfile.ZipFile object>
# Extract to specific directory
zip_file = fu.unzip("/path/to/archive.zip", out_path="/path/to/extract")
print(zip_file) # <zipfile.ZipFile object>
REMOVE
Remove the given file or directory and return True if it was successfully removed, False otherwise.
from ddcUtils import FileUtils
fu = FileUtils()
# Remove a file
success = fu.remove("/path/to/unwanted_file.txt")
print(success) # True
# Remove a directory and all its contents
success = fu.remove("/path/to/unwanted_directory")
print(success) # True
RENAME
Rename the given file or directory and returns True if the operation was successful, False otherwise.
from ddcUtils import FileUtils
fu = FileUtils()
# Rename a file
success = fu.rename("/path/to/old_name.txt", "/path/to/new_name.txt")
print(success) # True
# Rename a directory
success = fu.rename("/path/to/old_folder", "/path/to/new_folder")
print(success) # True
COPY_DIR
Copy all files and subdirectories from source to destination directory and return True for success or False for failure.
from ddcUtils import FileUtils
fu = FileUtils()
# Copy directory with all contents
success = fu.copy_dir("/path/to/source_dir", "/path/to/destination_dir")
print(success) # True
# Copy directory including symbolic links
success = fu.copy_dir("/path/to/source_dir", "/path/to/destination_dir", symlinks=True)
print(success) # True
# Copy directory ignoring certain patterns
import shutil
success = fu.copy_dir("/path/to/source_dir", "/path/to/destination_dir",
ignore=shutil.ignore_patterns('*.tmp', '*.log'))
print(success) # True
DOWNLOAD_FILE
Download a file from a remote URL to a local file path and return True for success or False for failure.
from ddcUtils import FileUtils
fu = FileUtils()
# Download file from URL
success = fu.download_file(
"https://example.com/data/file.csv",
"/local/path/downloaded_file.csv"
)
print(success) # True
# Download image
success = fu.download_file(
"https://example.com/images/photo.jpg",
"/local/images/photo.jpg"
)
print(success) # True
GET_EXE_BINARY_TYPE
Analyzes a Windows executable file and returns its binary type (32-bit or 64-bit architecture).
from ddcUtils import FileUtils
fu = FileUtils()
# Check Windows executable architecture
binary_type = fu.get_exe_binary_type("C:\\Program Files\\app.exe")
print(binary_type) # "64-bit" or "32-bit"
# Check another executable
binary_type = fu.get_exe_binary_type("C:\\Windows\\System32\\notepad.exe")
print(binary_type) # "64-bit"
IS_OLDER_THAN_X_DAYS
Check if a file or directory is older than the specified number of days and returns True or False.
from ddcUtils import FileUtils
fu = FileUtils()
# Check if the file is older than 30 days
is_old = fu.is_older_than_x_days("/path/to/log_file.txt", 30)
print(is_old) # True or False
# Check if the directory is older than 7 days
is_old = fu.is_older_than_x_days("/path/to/temp_folder", 7)
print(is_old) # True or False
# Useful for cleanup scripts
if fu.is_older_than_x_days("/path/to/backup.zip", 90):
fu.remove("/path/to/backup.zip")
print("Old backup removed")
COPY
Copy a single file from source path to destination path.
from ddcUtils import FileUtils
fu = FileUtils()
# Copy single file
success = fu.copy("/path/to/source_file.txt", "/path/to/destination_file.txt")
print(success) # True
# Copy file to different directory
success = fu.copy("/home/user/document.pdf", "/backup/document.pdf")
print(success) # True
Object
This class is used for creating a simple dynamic object that allows you to add attributes on the fly.
from ddcUtils import Object
# Create dynamic object
obj = Object()
obj.name = "John Doe"
obj.age = 30
obj.email = "john@example.com"
print(obj.name) # "John Doe"
print(obj.age) # 30
# Use as configuration object
config = Object()
config.database_url = "postgresql://localhost:5432/mydb"
config.debug_mode = True
config.max_connections = 100
print(config.database_url) # "postgresql://localhost:5432/mydb"
Misc Utils
CLEAR_SCREEN
Clears the terminal/console screen, works cross-platform (Windows, Linux, macOS).
from ddcUtils import MiscUtils
mu = MiscUtils()
# Clear terminal screen (works on Windows, Linux, macOS)
mu.clear_screen()
print("Screen cleared!")
# Useful in interactive scripts
while True:
user_input = input("Enter command (or 'clear' to clear screen): ")
if user_input == 'clear':
mu.clear_screen()
elif user_input == 'quit':
break
USER_CHOICE
Presents options to the user and prompts them to select one, returning the user's choice.
from ddcUtils import MiscUtils
mu = MiscUtils()
# Present menu options to user
options = ["Create new file", "Edit existing file", "Delete file", "Exit"]
choice = mu.user_choice(options)
print(f"You selected: {choice}") # User's selection
# Simple yes/no choice
yes_no = mu.user_choice(["Yes", "No"])
if yes_no == "Yes":
print("Proceeding...")
else:
print("Cancelled.")
GET_ACTIVE_BRANCH_NAME
Returns the name of the currently active Git branch if found, otherwise returns None.
from ddcUtils import MiscUtils
mu = MiscUtils()
# Get current Git branch in current directory
branch = mu.get_active_branch_name()
print(branch) # "main" or "develop" or None
# Get branch from a specific Git repository
branch = mu.get_active_branch_name(git_dir="/path/to/project/.git")
print(branch) # "feature/new-feature" or None
# Use in deployment scripts
current_branch = mu.get_active_branch_name()
if current_branch == "main":
print("Deploying to production...")
elif current_branch == "develop":
print("Deploying to staging...")
else:
print(f"Branch '{current_branch}' not configured for deployment")
GET_CURRENT_DATE_TIME
Returns the current date and time as a datetime object in UTC timezone.
from ddcUtils import MiscUtils
from datetime import datetime
mu = MiscUtils()
# Get current UTC datetime
current_time = mu.get_current_date_time()
print(current_time) # 2024-01-15 14:30:25.123456+00:00
print(type(current_time)) # <class 'datetime.datetime'>
# Use for timestamps
timestamp = mu.get_current_date_time()
print(f"Operation completed at: {timestamp}")
CONVERT_DATETIME_TO_STR_LONG
Converts a datetime object to a long string format.
Returns: "Mon Jan 01 2024 21:43:04"
from ddcUtils import MiscUtils
from datetime import datetime
mu = MiscUtils()
# Convert datetime to long string format
dt = datetime(2024, 1, 15, 21, 43, 4)
long_str = mu.convert_datetime_to_str_long(dt)
print(long_str) # "Mon Jan 15 2024 21:43:04"
# Use with current time
current_time = mu.get_current_date_time()
formatted = mu.convert_datetime_to_str_long(current_time)
print(f"Current time: {formatted}")
CONVERT_DATETIME_TO_STR_SHORT
Converts a datetime object to a short string format.
Returns: "2024-01-01 00:00:00.000000"
from ddcUtils import MiscUtils
from datetime import datetime
mu = MiscUtils()
# Convert datetime to short string format
dt = datetime(2024, 1, 15, 12, 30, 45, 123456)
short_str = mu.convert_datetime_to_str_short(dt)
print(short_str) # "2024-01-15 12:30:45.123456"
# Use for logging
current_time = mu.get_current_date_time()
log_timestamp = mu.convert_datetime_to_str_short(current_time)
print(f"[{log_timestamp}] Application started")
CONVERT_STR_TO_DATETIME_SHORT
Converts a string to a datetime object.
Input format: "2024-01-01 00:00:00.000000"
from ddcUtils import MiscUtils
mu = MiscUtils()
# Convert string to datetime object
date_str = "2024-01-15 12:30:45.123456"
dt = mu.convert_str_to_datetime_short(date_str)
print(dt) # 2024-01-15 12:30:45.123456
print(type(dt)) # <class 'datetime.datetime'>
# Parse timestamps from logs
log_entry = "2024-01-15 09:15:30.000000"
parsed_time = mu.convert_str_to_datetime_short(log_entry)
print(f"Log entry time: {parsed_time}")
GET_CURRENT_DATE_TIME_STR_LONG
Returns the current date and time as a long formatted string.
Returns: "Mon Jan 01 2024 21:47:00"
from ddcUtils import MiscUtils
mu = MiscUtils()
# Get current time as long formatted string
current_str = mu.get_current_date_time_str_long()
print(current_str) # "Mon Jan 15 2024 21:47:00"
# Use for user-friendly timestamps
print(f"Report generated on: {mu.get_current_date_time_str_long()}")
# Use in file names
filename = f"backup_{mu.get_current_date_time_str_long().replace(' ', '_').replace(':', '-')}.zip"
print(filename) # "backup_Mon_Jan_15_2024_21-47-00.zip"
OS Utils
GET_OS_NAME
Get the operating system name (Windows, Linux, Darwin/macOS, etc.).
from ddcUtils import OsUtils
ou = OsUtils()
# Get operating system name
os_name = ou.get_os_name()
print(os_name) # "Windows", "Linux", "Darwin" (macOS), etc.
# Use for platform-specific logic
if ou.get_os_name() == "Windows":
print("Running on Windows")
# Windows-specific code
elif ou.get_os_name() == "Linux":
print("Running on Linux")
# Linux-specific code
elif ou.get_os_name() == "Darwin":
print("Running on macOS")
# macOS-specific code
IS_WINDOWS
Check if the current operating system is Windows and returns True or False.
from ddcUtils import OsUtils
ou = OsUtils()
# Check if running on Windows
is_win = ou.is_windows()
print(is_win) # True or False
# Use for Windows-specific operations
if ou.is_windows():
print("Configuring Windows-specific settings...")
# Use backslashes for paths, configure Windows services, etc.
else:
print("Configuring Unix-like system settings...")
# Use forward slashes, configure Unix services, etc.
GET_CURRENT_PATH
Returns the current working directory as a string path.
from ddcUtils import OsUtils
ou = OsUtils()
# Get the current working directory
current_dir = ou.get_current_path()
print(current_dir) # "/home/user/projects/myapp" or "C:\\Users\\User\\Projects\\MyApp"
# Use for relative path operations
config_file = f"{ou.get_current_path()}/config.ini"
print(f"Config file location: {config_file}")
# Create paths relative to the current directory
log_dir = f"{ou.get_current_path()}/logs"
data_dir = f"{ou.get_current_path()}/data"
GET_PICTURES_PATH
Returns the path to the Pictures directory inside the user's home directory.
from ddcUtils import OsUtils
ou = OsUtils()
# Get user's Pictures directory
pictures_dir = ou.get_pictures_path()
print(pictures_dir) # "/home/user/Pictures" or "C:\\Users\\User\\Pictures"
# Use for saving images
from ddcUtils import FileUtils
fu = FileUtils()
# Download image to Pictures folder
image_path = f"{ou.get_pictures_path()}/downloaded_image.jpg"
success = fu.download_file("https://example.com/image.jpg", image_path)
if success:
print(f"Image saved to: {image_path}")
GET_DOWNLOADS_PATH
Returns the path to the Downloads directory inside the user's home directory.
from ddcUtils import OsUtils
ou = OsUtils()
# Get user's Downloads directory
downloads_dir = ou.get_downloads_path()
print(downloads_dir) # "/home/user/Downloads" or "C:\\Users\\User\\Downloads"
# Use for file downloads
from ddcUtils import FileUtils
fu = FileUtils()
# Download file to Downloads folder
file_path = f"{ou.get_downloads_path()}/document.pdf"
success = fu.download_file("https://example.com/document.pdf", file_path)
if success:
print(f"File downloaded to: {file_path}")
# Check for old downloads
if fu.is_older_than_x_days(downloads_dir, 30):
print("Downloads folder has old files")
Development
Building from Source
poetry build -f wheel
Running Tests
poetry update --with test
poe tests
License
Released under the MIT License
Support
If you find this project helpful, consider supporting development:
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 Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ddcutils-2.0.0.tar.gz.
File metadata
- Download URL: ddcutils-2.0.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea03f00c083bf1c5c547c13d1f48fc5398ae22d594591aec99779805fe57514e
|
|
| MD5 |
6f94668b74413e9d07d0abca27e35b7e
|
|
| BLAKE2b-256 |
5cf4ef1f094712af24b1a2a0192fe12791717afc4423615eb1c13eae3a0fb409
|
File details
Details for the file ddcutils-2.0.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: ddcutils-2.0.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 13.8 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a6180a1a9a683bdecafa49673db048209e5e6c47f209487c68b4841994fdc01
|
|
| MD5 |
6344cb406b24ba4df0ba443c1f31c9b3
|
|
| BLAKE2b-256 |
447c72205793ac7f6aeb0cdee66f07ca15412c8566e5c8f98e05dda8ca00ae43
|
File details
Details for the file ddcutils-2.0.0-cp313-cp313-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: ddcutils-2.0.0-cp313-cp313-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 13.7 kB
- Tags: CPython 3.13, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a33509697162221ece08b903d48c5b61928d72619173b07140457e00c4bb479
|
|
| MD5 |
75053b18700b48c95f1dd61b4ea518b2
|
|
| BLAKE2b-256 |
1f42da0a4cd8b2509403dc5a28212cf123bc01d7ef43db4af24093c97dee7078
|
File details
Details for the file ddcutils-2.0.0-cp313-cp313-macosx_14_0_arm64.whl.
File metadata
- Download URL: ddcutils-2.0.0-cp313-cp313-macosx_14_0_arm64.whl
- Upload date:
- Size: 13.7 kB
- Tags: CPython 3.13, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6550691d068bacc7f3180b7123d91c973b3d284cc988c343dde886613d2c62e
|
|
| MD5 |
55350f1615a26a3741e625ce3215d147
|
|
| BLAKE2b-256 |
4ba54b74aa92d43d50575c40b022cf68a5fb146cd70ece40b03d2aa37260f277
|
File details
Details for the file ddcutils-2.0.0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: ddcutils-2.0.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 13.8 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60d662d4a793cb5ca6cf824ea386f95df533fe9b94764a5c99cd15d5a4bb9ab7
|
|
| MD5 |
f7d3818e6cda9b8fcdbf52f1411367d1
|
|
| BLAKE2b-256 |
96c23a7deceba16eb7c95ebb1c5f8e6a92851b2126f0785efd7896296ff7600a
|
File details
Details for the file ddcutils-2.0.0-cp312-cp312-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: ddcutils-2.0.0-cp312-cp312-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 13.8 kB
- Tags: CPython 3.12, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b35e1cdfecf09a2bfb32ce9c5e1dabce29b749490940471c0c3cd99d197d9c87
|
|
| MD5 |
95b5d38e570193afc3c1f5605e0ae4f7
|
|
| BLAKE2b-256 |
64aaad6e1b40ecc8b05824496266785434f571000d79de83295f6f336354a8e1
|
File details
Details for the file ddcutils-2.0.0-cp312-cp312-macosx_14_0_arm64.whl.
File metadata
- Download URL: ddcutils-2.0.0-cp312-cp312-macosx_14_0_arm64.whl
- Upload date:
- Size: 13.7 kB
- Tags: CPython 3.12, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d86bdc172eb47603457738cb9fd9f2d48658b5bad3c6ae683a7d2d98f835126
|
|
| MD5 |
c09999307992a1ba1d21b73964ce1e73
|
|
| BLAKE2b-256 |
880c802575044ea4e04824a7aeb0733129f63bb3d55bf0271a4e42e3182eab3f
|