Project Overview 💡
In this project, you’ll write a Python script that appends the current date to all .txt
filenames in a specified folder. This is useful for version control, organizing files by date, or backing up daily logs.
Challenge Yourself! 🚀
Before checking the solution, try building a script that scans a folder and renames each .txt
file by adding today's date at the end.
Task:
Write a Python program that:
Goes through each
.txt
file in a directoryAppends the current date to the filename
For example, file a.txt should be renamed to a-2025-5-8.txt (existing_name-YYYY-MM-DD.txt)You can download and use these sample files and place them in a directory named “files”.
Expected Output:
The console will display the old and new names of each file.
And here is how the renamed files look like:
Give it a shot! Scroll down when you're ready for the step-by-step guide.
Spoiler Alert!
Step-by-Step Guide
Step 1️⃣: Import Required Modules
We’ll use os
for working with files and datetime
for getting the current date.
import os
from datetime import datetime
Step 2️⃣: Set the Target Directory
Specify the directory where your .txt
files are stored.