Python First Assignment
Python First Assignment
--------------------------------------------------------------------------------------------
Program-1 Renaming files from American-style dates to European-style
Main program
import os
import re
import shu l
# Extract parts
before = match.group("before")
month = match.group("month").zfill(2)
day = match.group("day").zfill(2)
year = match.group("year")
a er = match.group("a er")
# Full paths
old_path = os.path.join(folder_path, filename)
new_path = os.path.join(folder_path, european_filename)
A er
The program converts the file names with American style date to
European style dates
1. Sets a target folder (test_file) where files are located.
2. Defines a regex pa ern to find filenames containing American-
style dates (MM-DD-YYYY).
3. Loops through all files in the target folder.
4. For each file, checks if the filename matches the American date
pa ern.
5. If matched, extracts the date parts (month, day, year) and
surrounding text.
6. Converts the date format from American (MM-DD-YYYY) to
European (DD-MM-YYYY).
7. Constructs a new filename with the converted date format.
8. Prints out the intended rename opera on (old filename → new
filename).
9. If dry_run is set to False, actually renames the files on disk using
shu l.move.
10. Otherwise, if dry_run is True, it only simulates the rename
by prin ng.
Program-2 Backing up a folder into a zip file.
Main program
Output
Before Execu ng
A er Execu ng