Created
March 10, 2024 20:10
-
-
Save Darkflib/42da7cb32514aa30b4303a22af7ffd29 to your computer and use it in GitHub Desktop.
fix-names.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import logging | |
# Configure logging | |
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') | |
def sanitize_filename(filename): | |
"""Remove leading dots from the filename.""" | |
return filename.lstrip('.') | |
def rename_files_in_directory(directory): | |
"""Rename files in the given directory by removing initial dots.""" | |
try: | |
for filename in os.listdir(directory): | |
if filename.startswith('.'): | |
sanitized_name = sanitize_filename(filename) | |
original_path = os.path.join(directory, filename) | |
new_path = os.path.join(directory, sanitized_name) | |
try: | |
os.rename(original_path, new_path) | |
logging.info(f'Renamed "{filename}" to "{sanitized_name}"') | |
except Exception as e: | |
logging.error(f'Error renaming "{filename}" to "{sanitized_name}": {e}') | |
except Exception as e: | |
logging.error(f'Error accessing directory "{directory}": {e}') | |
# Specify your directory path here | |
directory_path = '.' | |
rename_files_in_directory(directory_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment