To view the full archive of past projects, visit the Daily Python Projects homepage at https://dailypythonprojects.substack.com
Subscribe below to receive a new Python project every day by email:
1. Project Brief
Your task for this project is to batch-rename the files of a directory.
2. Project Instructions
Create an empty directory manually.
Download and place these files in that directory.
Create an empty Python script outside of that directory.
Write a program in that script that renames all the files of the directory.
The new names should contain the current date (e.g., rename from a.txt to a-2024-3-24.txt)
3. Environment Setup Instructions
To run the code, execute the main.py file with:
python run main.py
4. Resources
Learn how to rename a file in the article below:
https://pythonhow.com/how/rename-a-file/
Hi Ardit
My solution: https://pastebin.com/1C4a2fx8
I used sys.argv
So when you run the file you have to specify the directory the files are in e.g.:
$ python main.py files
Another more generic way if we want to change the names of all files in the dir:
for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
filepath = os.path.join(root, file)
name, ext = os.path.splitext(filepath)
os.rename(filepath, f'{name}-{today}{ext}')