# File Utilities
Few File Utilities and some OS Functions
[](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)
[](https://opensource.org/licenses/MIT)
[](https://pypi.python.org/pypi/ddcUtils)
[](https://pepy.tech/projects/ddcUtils)
[](https://codecov.io/gh/ddc/ddcUtils)
[](https://github.com/ddc/ddcUtils/actions/workflows/workflow.yml)
[](https://sonarcloud.io/dashboard?id=ddc_ddcUtils)
[](https://actions-badge.atrox.dev/ddc/ddcUtils/goto?ref=main)
[](https://github.com/psf/black)
[](https://www.python.org/downloads)
[](https://github.com/sponsors/ddc)
## Table of Contents
- [Install](#install)
- [Conf File Utils](#conf-file-utils)
- [File Utils](#file-utils)
- [Object](#object)
- [Misc Utils](#misc-utils)
- [OS Utils](#os-utils)
- [Development](#development)
- [License](#license)
- [Support](#support)
# Install
```shell
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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).
```python
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.
```python
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.
```python
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.
```python
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).
```python
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.
```python
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.
```python
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.
```python
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"`
```python
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"`
```python
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"`
```python
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"`
```python
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.).
```python
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.
```python
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.
```python
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.
```python
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.
```python
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
```shell
poetry build -f wheel
```
### Running Tests
```shell
poetry update --with test
poe tests
```
# License
Released under the [MIT License](LICENSE)
# Support
If you find this project helpful, consider supporting development:
- [GitHub Sponsor](https://github.com/sponsors/ddc)
- [ko-fi](https://ko-fi.com/ddcsta)
- [PayPal](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)
Raw data
{
"_id": null,
"home_page": null,
"name": "ddcUtils",
"maintainer": "Daniel Costa",
"docs_url": null,
"requires_python": "<4.0,>=3.12",
"maintainer_email": null,
"keywords": "python3, python-3, python, tools, utility, utilities, utils, utility-library, utils-library, utilities-library",
"author": "Daniel Costa",
"author_email": "danieldcsta@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/5c/f4/ef1f094712af24b1a2a0192fe12791717afc4423615eb1c13eae3a0fb409/ddcutils-2.0.0.tar.gz",
"platform": null,
"description": "# File Utilities\nFew File Utilities and some OS Functions\n\n[](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)\n[](https://opensource.org/licenses/MIT)\n[](https://pypi.python.org/pypi/ddcUtils)\n[](https://pepy.tech/projects/ddcUtils)\n[](https://codecov.io/gh/ddc/ddcUtils)\n[](https://github.com/ddc/ddcUtils/actions/workflows/workflow.yml)\n[](https://sonarcloud.io/dashboard?id=ddc_ddcUtils) \n[](https://actions-badge.atrox.dev/ddc/ddcUtils/goto?ref=main)\n[](https://github.com/psf/black)\n[](https://www.python.org/downloads)\n\n[](https://github.com/sponsors/ddc)\n\n\n## Table of Contents\n- [Install](#install)\n- [Conf File Utils](#conf-file-utils)\n- [File Utils](#file-utils)\n- [Object](#object)\n- [Misc Utils](#misc-utils)\n- [OS Utils](#os-utils)\n- [Development](#development)\n- [License](#license)\n- [Support](#support)\n\n\n# Install\n```shell\npip install ddcUtils\n```\n\n\n# Conf File Utils\n\nFile example - file.ini:\n\n [main]\n files=5\n path=\"/tmp/test_dir\"\n port=5432\n list=1,2,3,4,5,6\n\n\n### GET_ALL_VALUES\nGet all values from an .ini config file structure and return them as a dictionary.\\\nThe `mixed_values` parameter will return all values as an object instead of dict.\n```python\nfrom ddcUtils import ConfFileUtils\n\ncfu = ConfFileUtils()\n# Get all values as dictionary\nall_values = cfu.get_all_values(\"/path/to/config.ini\", mixed_values=False)\nprint(all_values) # {'main': {'files': '5', 'path': '/tmp/test_dir', 'port': '5432', 'list': '1,2,3,4,5,6'}}\n\n# Get all values as object\nall_values_obj = cfu.get_all_values(\"/path/to/config.ini\", mixed_values=True)\nprint(all_values_obj.main.files) # 5\n```\n\n\n\n### GET_SECTION_VALUES\nGet all section values from an .ini config file structure and return them as a dictionary.\n```python\nfrom ddcUtils import ConfFileUtils\n\ncfu = ConfFileUtils()\n# Get all values from 'main' section\nsection_values = cfu.get_section_values(\"/path/to/config.ini\", \"main\")\nprint(section_values) # {'files': '5', 'path': '/tmp/test_dir', 'port': '5432', 'list': '1,2,3,4,5,6'}\n```\n\n\n\n### GET_VALUE\nGet a specific value from an .ini config file structure and return it.\n```python\nfrom ddcUtils import ConfFileUtils\n\ncfu = ConfFileUtils()\n# Get specific value from config\nport = cfu.get_value(\"/path/to/config.ini\", \"main\", \"port\")\nprint(port) # \"5432\"\n\npath = cfu.get_value(\"/path/to/config.ini\", \"main\", \"path\")\nprint(path) # \"/tmp/test_dir\"\n```\n\n\n\n### SET_VALUE\nSet a value in an .ini config file structure and return True for success or False for failure.\n```python\nfrom ddcUtils import ConfFileUtils\n\ncfu = ConfFileUtils()\n# Set a new value in config file\nsuccess = cfu.set_value(\"/path/to/config.ini\", \"main\", \"port\", \"8080\")\nprint(success) # True\n\n# Set a list value with comma separation\nsuccess = cfu.set_value(\"/path/to/config.ini\", \"main\", \"servers\", \"server1,server2,server3\", commas=True)\nprint(success) # True\n```\n\n\n# File Utils\n\n### OPEN\nOpen 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.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Open file in default application\nsuccess = fu.open(\"/path/to/document.txt\")\nprint(success) # True\n\n# Open directory in file explorer\nsuccess = fu.open(\"/path/to/directory\")\nprint(success) # True\n```\n\n\n\n### LIST_FILES\nList all files in the given directory and return them in a tuple sorted by creation time in ascending order.\\\nSupports filtering by file prefix and suffix.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# List all files in directory\nall_files = fu.list_files(\"/home/user/documents\")\nprint(all_files) # ('file1.txt', 'file2.pdf', 'image.jpg')\n\n# List files starting with 'test'\ntest_files = fu.list_files(\"/home/user/documents\", starts_with=\"test\")\nprint(test_files) # ('test_data.csv', 'test_results.txt')\n\n# List Python files\npy_files = fu.list_files(\"/home/user/projects\", ends_with=\".py\")\nprint(py_files) # ('main.py', 'utils.py', 'config.py')\n```\n\n\n\n### GZIP\nCompress the given file using gzip compression and returns the Path object for success or None if the operation failed.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Compress file to the same directory\ncompressed_path = fu.gzip(\"/path/to/large_file.txt\")\nprint(compressed_path) # /path/to/large_file.txt.gz\n\n# Compress file to specific directory\ncompressed_path = fu.gzip(\"/path/to/large_file.txt\", output_dir=\"/path/to/compressed\")\nprint(compressed_path) # /path/to/compressed/large_file.txt.gz\n```\n\n\n\n### UNZIP\nExtracts the contents of a ZIP file to the specified output directory and returns ZipFile object for success or None if the operation failed.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Extract to the same directory as ZIP file\nzip_file = fu.unzip(\"/path/to/archive.zip\")\nprint(zip_file) # <zipfile.ZipFile object>\n\n# Extract to specific directory\nzip_file = fu.unzip(\"/path/to/archive.zip\", out_path=\"/path/to/extract\")\nprint(zip_file) # <zipfile.ZipFile object>\n```\n\n\n\n### REMOVE\nRemove the given file or directory and return True if it was successfully removed, False otherwise.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Remove a file\nsuccess = fu.remove(\"/path/to/unwanted_file.txt\")\nprint(success) # True\n\n# Remove a directory and all its contents\nsuccess = fu.remove(\"/path/to/unwanted_directory\")\nprint(success) # True\n```\n\n\n\n### RENAME\nRename the given file or directory and returns True if the operation was successful, False otherwise.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Rename a file\nsuccess = fu.rename(\"/path/to/old_name.txt\", \"/path/to/new_name.txt\")\nprint(success) # True\n\n# Rename a directory\nsuccess = fu.rename(\"/path/to/old_folder\", \"/path/to/new_folder\")\nprint(success) # True\n```\n\n\n\n### COPY_DIR\nCopy all files and subdirectories from source to destination directory and return True for success or False for failure.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Copy directory with all contents\nsuccess = fu.copy_dir(\"/path/to/source_dir\", \"/path/to/destination_dir\")\nprint(success) # True\n\n# Copy directory including symbolic links\nsuccess = fu.copy_dir(\"/path/to/source_dir\", \"/path/to/destination_dir\", symlinks=True)\nprint(success) # True\n\n# Copy directory ignoring certain patterns\nimport shutil\nsuccess = fu.copy_dir(\"/path/to/source_dir\", \"/path/to/destination_dir\", \n ignore=shutil.ignore_patterns('*.tmp', '*.log'))\nprint(success) # True\n```\n\n\n\n### DOWNLOAD_FILE\nDownload a file from a remote URL to a local file path and return True for success or False for failure.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Download file from URL\nsuccess = fu.download_file(\n \"https://example.com/data/file.csv\", \n \"/local/path/downloaded_file.csv\"\n)\nprint(success) # True\n\n# Download image\nsuccess = fu.download_file(\n \"https://example.com/images/photo.jpg\", \n \"/local/images/photo.jpg\"\n)\nprint(success) # True\n```\n\n\n\n### GET_EXE_BINARY_TYPE\nAnalyzes a Windows executable file and returns its binary type (32-bit or 64-bit architecture).\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Check Windows executable architecture\nbinary_type = fu.get_exe_binary_type(\"C:\\\\Program Files\\\\app.exe\")\nprint(binary_type) # \"64-bit\" or \"32-bit\"\n\n# Check another executable\nbinary_type = fu.get_exe_binary_type(\"C:\\\\Windows\\\\System32\\\\notepad.exe\")\nprint(binary_type) # \"64-bit\"\n```\n\n\n\n### IS_OLDER_THAN_X_DAYS\nCheck if a file or directory is older than the specified number of days and returns True or False.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Check if the file is older than 30 days\nis_old = fu.is_older_than_x_days(\"/path/to/log_file.txt\", 30)\nprint(is_old) # True or False\n\n# Check if the directory is older than 7 days\nis_old = fu.is_older_than_x_days(\"/path/to/temp_folder\", 7)\nprint(is_old) # True or False\n\n# Useful for cleanup scripts\nif fu.is_older_than_x_days(\"/path/to/backup.zip\", 90):\n fu.remove(\"/path/to/backup.zip\")\n print(\"Old backup removed\")\n```\n\n\n\n### COPY\nCopy a single file from source path to destination path.\n```python\nfrom ddcUtils import FileUtils\n\nfu = FileUtils()\n# Copy single file\nsuccess = fu.copy(\"/path/to/source_file.txt\", \"/path/to/destination_file.txt\")\nprint(success) # True\n\n# Copy file to different directory\nsuccess = fu.copy(\"/home/user/document.pdf\", \"/backup/document.pdf\")\nprint(success) # True\n```\n\n\n\n# Object\nThis class is used for creating a simple dynamic object that allows you to add attributes on the fly.\n\n```python\nfrom ddcUtils import Object\n\n# Create dynamic object\nobj = Object()\nobj.name = \"John Doe\"\nobj.age = 30\nobj.email = \"john@example.com\"\n\nprint(obj.name) # \"John Doe\"\nprint(obj.age) # 30\n\n# Use as configuration object\nconfig = Object()\nconfig.database_url = \"postgresql://localhost:5432/mydb\"\nconfig.debug_mode = True\nconfig.max_connections = 100\n\nprint(config.database_url) # \"postgresql://localhost:5432/mydb\"\n``` \n\n\n# Misc Utils\n\n### CLEAR_SCREEN\nClears the terminal/console screen, works cross-platform (Windows, Linux, macOS).\n```python\nfrom ddcUtils import MiscUtils\n\nmu = MiscUtils()\n# Clear terminal screen (works on Windows, Linux, macOS)\nmu.clear_screen()\nprint(\"Screen cleared!\")\n\n# Useful in interactive scripts\nwhile True:\n user_input = input(\"Enter command (or 'clear' to clear screen): \")\n if user_input == 'clear':\n mu.clear_screen()\n elif user_input == 'quit':\n break\n```\n\n\n\n### USER_CHOICE\nPresents options to the user and prompts them to select one, returning the user's choice.\n```python\nfrom ddcUtils import MiscUtils\n\nmu = MiscUtils()\n# Present menu options to user\noptions = [\"Create new file\", \"Edit existing file\", \"Delete file\", \"Exit\"]\nchoice = mu.user_choice(options)\nprint(f\"You selected: {choice}\") # User's selection\n\n# Simple yes/no choice\nyes_no = mu.user_choice([\"Yes\", \"No\"])\nif yes_no == \"Yes\":\n print(\"Proceeding...\")\nelse:\n print(\"Cancelled.\")\n```\n\n\n\n### GET_ACTIVE_BRANCH_NAME\nReturns the name of the currently active Git branch if found, otherwise returns None.\n```python\nfrom ddcUtils import MiscUtils\n\nmu = MiscUtils()\n# Get current Git branch in current directory\nbranch = mu.get_active_branch_name()\nprint(branch) # \"main\" or \"develop\" or None\n\n# Get branch from a specific Git repository\nbranch = mu.get_active_branch_name(git_dir=\"/path/to/project/.git\")\nprint(branch) # \"feature/new-feature\" or None\n\n# Use in deployment scripts\ncurrent_branch = mu.get_active_branch_name()\nif current_branch == \"main\":\n print(\"Deploying to production...\")\nelif current_branch == \"develop\":\n print(\"Deploying to staging...\")\nelse:\n print(f\"Branch '{current_branch}' not configured for deployment\")\n```\n\n\n\n### GET_CURRENT_DATE_TIME\nReturns the current date and time as a datetime object in UTC timezone.\n```python\nfrom ddcUtils import MiscUtils\nfrom datetime import datetime\n\nmu = MiscUtils()\n# Get current UTC datetime\ncurrent_time = mu.get_current_date_time()\nprint(current_time) # 2024-01-15 14:30:25.123456+00:00\nprint(type(current_time)) # <class 'datetime.datetime'>\n\n# Use for timestamps\ntimestamp = mu.get_current_date_time()\nprint(f\"Operation completed at: {timestamp}\")\n```\n\n\n\n### CONVERT_DATETIME_TO_STR_LONG\nConverts a datetime object to a long string format.\n\nReturns: `\"Mon Jan 01 2024 21:43:04\"`\n```python\nfrom ddcUtils import MiscUtils\nfrom datetime import datetime\n\nmu = MiscUtils()\n# Convert datetime to long string format\ndt = datetime(2024, 1, 15, 21, 43, 4)\nlong_str = mu.convert_datetime_to_str_long(dt)\nprint(long_str) # \"Mon Jan 15 2024 21:43:04\"\n\n# Use with current time\ncurrent_time = mu.get_current_date_time()\nformatted = mu.convert_datetime_to_str_long(current_time)\nprint(f\"Current time: {formatted}\")\n```\n\n\n\n### CONVERT_DATETIME_TO_STR_SHORT\nConverts a datetime object to a short string format.\n\nReturns: `\"2024-01-01 00:00:00.000000\"`\n```python\nfrom ddcUtils import MiscUtils\nfrom datetime import datetime\n\nmu = MiscUtils()\n# Convert datetime to short string format\ndt = datetime(2024, 1, 15, 12, 30, 45, 123456)\nshort_str = mu.convert_datetime_to_str_short(dt)\nprint(short_str) # \"2024-01-15 12:30:45.123456\"\n\n# Use for logging\ncurrent_time = mu.get_current_date_time()\nlog_timestamp = mu.convert_datetime_to_str_short(current_time)\nprint(f\"[{log_timestamp}] Application started\")\n```\n\n\n\n### CONVERT_STR_TO_DATETIME_SHORT\nConverts a string to a datetime object.\n\nInput format: `\"2024-01-01 00:00:00.000000\"`\n```python\nfrom ddcUtils import MiscUtils\n\nmu = MiscUtils()\n# Convert string to datetime object\ndate_str = \"2024-01-15 12:30:45.123456\"\ndt = mu.convert_str_to_datetime_short(date_str)\nprint(dt) # 2024-01-15 12:30:45.123456\nprint(type(dt)) # <class 'datetime.datetime'>\n\n# Parse timestamps from logs\nlog_entry = \"2024-01-15 09:15:30.000000\"\nparsed_time = mu.convert_str_to_datetime_short(log_entry)\nprint(f\"Log entry time: {parsed_time}\")\n```\n\n\n\n### GET_CURRENT_DATE_TIME_STR_LONG\nReturns the current date and time as a long formatted string.\n\nReturns: `\"Mon Jan 01 2024 21:47:00\"`\n```python\nfrom ddcUtils import MiscUtils\n\nmu = MiscUtils()\n# Get current time as long formatted string\ncurrent_str = mu.get_current_date_time_str_long()\nprint(current_str) # \"Mon Jan 15 2024 21:47:00\"\n\n# Use for user-friendly timestamps\nprint(f\"Report generated on: {mu.get_current_date_time_str_long()}\")\n\n# Use in file names\nfilename = f\"backup_{mu.get_current_date_time_str_long().replace(' ', '_').replace(':', '-')}.zip\"\nprint(filename) # \"backup_Mon_Jan_15_2024_21-47-00.zip\"\n```\n\n\n# OS Utils\n\n### GET_OS_NAME\nGet the operating system name (Windows, Linux, Darwin/macOS, etc.).\n```python\nfrom ddcUtils import OsUtils\n\nou = OsUtils()\n# Get operating system name\nos_name = ou.get_os_name()\nprint(os_name) # \"Windows\", \"Linux\", \"Darwin\" (macOS), etc.\n\n# Use for platform-specific logic\nif ou.get_os_name() == \"Windows\":\n print(\"Running on Windows\")\n # Windows-specific code\nelif ou.get_os_name() == \"Linux\":\n print(\"Running on Linux\")\n # Linux-specific code\nelif ou.get_os_name() == \"Darwin\":\n print(\"Running on macOS\")\n # macOS-specific code\n```\n\n\n\n### IS_WINDOWS\nCheck if the current operating system is Windows and returns True or False.\n```python\nfrom ddcUtils import OsUtils\n\nou = OsUtils()\n# Check if running on Windows\nis_win = ou.is_windows()\nprint(is_win) # True or False\n\n# Use for Windows-specific operations\nif ou.is_windows():\n print(\"Configuring Windows-specific settings...\")\n # Use backslashes for paths, configure Windows services, etc.\nelse:\n print(\"Configuring Unix-like system settings...\")\n # Use forward slashes, configure Unix services, etc.\n```\n\n\n\n### GET_CURRENT_PATH\nReturns the current working directory as a string path.\n```python\nfrom ddcUtils import OsUtils\n\nou = OsUtils()\n# Get the current working directory\ncurrent_dir = ou.get_current_path()\nprint(current_dir) # \"/home/user/projects/myapp\" or \"C:\\\\Users\\\\User\\\\Projects\\\\MyApp\"\n\n# Use for relative path operations\nconfig_file = f\"{ou.get_current_path()}/config.ini\"\nprint(f\"Config file location: {config_file}\")\n\n# Create paths relative to the current directory\nlog_dir = f\"{ou.get_current_path()}/logs\"\ndata_dir = f\"{ou.get_current_path()}/data\"\n```\n\n\n\n### GET_PICTURES_PATH\nReturns the path to the Pictures directory inside the user's home directory.\n```python\nfrom ddcUtils import OsUtils\n\nou = OsUtils()\n# Get user's Pictures directory\npictures_dir = ou.get_pictures_path()\nprint(pictures_dir) # \"/home/user/Pictures\" or \"C:\\\\Users\\\\User\\\\Pictures\"\n\n# Use for saving images\nfrom ddcUtils import FileUtils\nfu = FileUtils()\n\n# Download image to Pictures folder\nimage_path = f\"{ou.get_pictures_path()}/downloaded_image.jpg\"\nsuccess = fu.download_file(\"https://example.com/image.jpg\", image_path)\nif success:\n print(f\"Image saved to: {image_path}\")\n```\n\n\n\n### GET_DOWNLOADS_PATH\nReturns the path to the Downloads directory inside the user's home directory.\n```python\nfrom ddcUtils import OsUtils\n\nou = OsUtils()\n# Get user's Downloads directory\ndownloads_dir = ou.get_downloads_path()\nprint(downloads_dir) # \"/home/user/Downloads\" or \"C:\\\\Users\\\\User\\\\Downloads\"\n\n# Use for file downloads\nfrom ddcUtils import FileUtils\nfu = FileUtils()\n\n# Download file to Downloads folder\nfile_path = f\"{ou.get_downloads_path()}/document.pdf\"\nsuccess = fu.download_file(\"https://example.com/document.pdf\", file_path)\nif success:\n print(f\"File downloaded to: {file_path}\")\n\n# Check for old downloads\nif fu.is_older_than_x_days(downloads_dir, 30):\n print(\"Downloads folder has old files\")\n```\n\n\n# Development\n\n### Building from Source\n```shell\npoetry build -f wheel\n```\n\n### Running Tests\n```shell\npoetry update --with test\npoe tests\n```\n\n\n\n# License\nReleased under the [MIT License](LICENSE)\n\n\n\n# Support\nIf you find this project helpful, consider supporting development:\n\n- [GitHub Sponsor](https://github.com/sponsors/ddc)\n- [ko-fi](https://ko-fi.com/ddcsta)\n- [PayPal](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Few File Utilities and some OS Functions",
"version": "2.0.0",
"project_urls": {
"Homepage": "https://pypi.org/project/ddcUtils",
"Repository": "https://github.com/ddc/ddcUtils"
},
"split_keywords": [
"python3",
" python-3",
" python",
" tools",
" utility",
" utilities",
" utils",
" utility-library",
" utils-library",
" utilities-library"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "880c802575044ea4e04824a7aeb0733129f63bb3d55bf0271a4e42e3182eab3f",
"md5": "c09999307992a1ba1d21b73964ce1e73",
"sha256": "2d86bdc172eb47603457738cb9fd9f2d48658b5bad3c6ae683a7d2d98f835126"
},
"downloads": -1,
"filename": "ddcutils-2.0.0-cp312-cp312-macosx_14_0_arm64.whl",
"has_sig": false,
"md5_digest": "c09999307992a1ba1d21b73964ce1e73",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": "<4.0,>=3.12",
"size": 13744,
"upload_time": "2025-07-27T14:36:22",
"upload_time_iso_8601": "2025-07-27T14:36:22.507221Z",
"url": "https://files.pythonhosted.org/packages/88/0c/802575044ea4e04824a7aeb0733129f63bb3d55bf0271a4e42e3182eab3f/ddcutils-2.0.0-cp312-cp312-macosx_14_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "64aaad6e1b40ecc8b05824496266785434f571000d79de83295f6f336354a8e1",
"md5": "95b5d38e570193afc3c1f5605e0ae4f7",
"sha256": "b35e1cdfecf09a2bfb32ce9c5e1dabce29b749490940471c0c3cd99d197d9c87"
},
"downloads": -1,
"filename": "ddcutils-2.0.0-cp312-cp312-manylinux_2_39_x86_64.whl",
"has_sig": false,
"md5_digest": "95b5d38e570193afc3c1f5605e0ae4f7",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": "<4.0,>=3.12",
"size": 13750,
"upload_time": "2025-07-27T14:36:23",
"upload_time_iso_8601": "2025-07-27T14:36:23.615572Z",
"url": "https://files.pythonhosted.org/packages/64/aa/ad6e1b40ecc8b05824496266785434f571000d79de83295f6f336354a8e1/ddcutils-2.0.0-cp312-cp312-manylinux_2_39_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "96c23a7deceba16eb7c95ebb1c5f8e6a92851b2126f0785efd7896296ff7600a",
"md5": "f7d3818e6cda9b8fcdbf52f1411367d1",
"sha256": "60d662d4a793cb5ca6cf824ea386f95df533fe9b94764a5c99cd15d5a4bb9ab7"
},
"downloads": -1,
"filename": "ddcutils-2.0.0-cp312-cp312-win_amd64.whl",
"has_sig": false,
"md5_digest": "f7d3818e6cda9b8fcdbf52f1411367d1",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": "<4.0,>=3.12",
"size": 13816,
"upload_time": "2025-07-27T14:36:24",
"upload_time_iso_8601": "2025-07-27T14:36:24.282195Z",
"url": "https://files.pythonhosted.org/packages/96/c2/3a7deceba16eb7c95ebb1c5f8e6a92851b2126f0785efd7896296ff7600a/ddcutils-2.0.0-cp312-cp312-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "4ba54b74aa92d43d50575c40b022cf68a5fb146cd70ece40b03d2aa37260f277",
"md5": "55350f1615a26a3741e625ce3215d147",
"sha256": "b6550691d068bacc7f3180b7123d91c973b3d284cc988c343dde886613d2c62e"
},
"downloads": -1,
"filename": "ddcutils-2.0.0-cp313-cp313-macosx_14_0_arm64.whl",
"has_sig": false,
"md5_digest": "55350f1615a26a3741e625ce3215d147",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": "<4.0,>=3.12",
"size": 13742,
"upload_time": "2025-07-27T14:36:25",
"upload_time_iso_8601": "2025-07-27T14:36:25.044495Z",
"url": "https://files.pythonhosted.org/packages/4b/a5/4b74aa92d43d50575c40b022cf68a5fb146cd70ece40b03d2aa37260f277/ddcutils-2.0.0-cp313-cp313-macosx_14_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "1f42da0a4cd8b2509403dc5a28212cf123bc01d7ef43db4af24093c97dee7078",
"md5": "75053b18700b48c95f1dd61b4ea518b2",
"sha256": "2a33509697162221ece08b903d48c5b61928d72619173b07140457e00c4bb479"
},
"downloads": -1,
"filename": "ddcutils-2.0.0-cp313-cp313-manylinux_2_39_x86_64.whl",
"has_sig": false,
"md5_digest": "75053b18700b48c95f1dd61b4ea518b2",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": "<4.0,>=3.12",
"size": 13747,
"upload_time": "2025-07-27T14:36:25",
"upload_time_iso_8601": "2025-07-27T14:36:25.743870Z",
"url": "https://files.pythonhosted.org/packages/1f/42/da0a4cd8b2509403dc5a28212cf123bc01d7ef43db4af24093c97dee7078/ddcutils-2.0.0-cp313-cp313-manylinux_2_39_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "447c72205793ac7f6aeb0cdee66f07ca15412c8566e5c8f98e05dda8ca00ae43",
"md5": "6344cb406b24ba4df0ba443c1f31c9b3",
"sha256": "8a6180a1a9a683bdecafa49673db048209e5e6c47f209487c68b4841994fdc01"
},
"downloads": -1,
"filename": "ddcutils-2.0.0-cp313-cp313-win_amd64.whl",
"has_sig": false,
"md5_digest": "6344cb406b24ba4df0ba443c1f31c9b3",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": "<4.0,>=3.12",
"size": 13816,
"upload_time": "2025-07-27T14:36:26",
"upload_time_iso_8601": "2025-07-27T14:36:26.682444Z",
"url": "https://files.pythonhosted.org/packages/44/7c/72205793ac7f6aeb0cdee66f07ca15412c8566e5c8f98e05dda8ca00ae43/ddcutils-2.0.0-cp313-cp313-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5cf4ef1f094712af24b1a2a0192fe12791717afc4423615eb1c13eae3a0fb409",
"md5": "6f94668b74413e9d07d0abca27e35b7e",
"sha256": "ea03f00c083bf1c5c547c13d1f48fc5398ae22d594591aec99779805fe57514e"
},
"downloads": -1,
"filename": "ddcutils-2.0.0.tar.gz",
"has_sig": false,
"md5_digest": "6f94668b74413e9d07d0abca27e35b7e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 17072,
"upload_time": "2025-07-27T14:36:27",
"upload_time_iso_8601": "2025-07-27T14:36:27.744649Z",
"url": "https://files.pythonhosted.org/packages/5c/f4/ef1f094712af24b1a2a0192fe12791717afc4423615eb1c13eae3a0fb409/ddcutils-2.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-27 14:36:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ddc",
"github_project": "ddcUtils",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ddcutils"
}