0% found this document useful (0 votes)
2 views6 pages

Python First Assignment

The document describes two Python programs: one that renames files from American-style dates (MM-DD-YYYY) to European-style dates (DD-MM-YYYY) and another that creates a backup of a folder by compressing its contents into a .zip file. The first program utilizes regex to identify and transform date formats in filenames, while the second program uses the zipfile module to archive folder contents. Both programs include options for dry runs and demonstrate file handling and manipulation in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python First Assignment

The document describes two Python programs: one that renames files from American-style dates (MM-DD-YYYY) to European-style dates (DD-MM-YYYY) and another that creates a backup of a folder by compressing its contents into a .zip file. The first program utilizes regex to identify and transform date formats in filenames, while the second program uses the zipfile module to archive folder contents. Both programs include options for dry runs and demonstrate file handling and manipulation in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment-1

--------------------------------------------------------------------------------------------
Program-1 Renaming files from American-style dates to European-style
Main program
import os
import re
import shu l

# --- CONFIGURATION ---


folder_path = '.' # Your folder containing the files
dry_run = False # Set to False a er checking output to rename for real

# --- Regex with named groups ---


date_pa ern = re.compile(r"""
(?P<before>.*?) # all text before the date
(?P<month>0?[1-9]|1[0-2]) # month: 1-12 with op onal 0
-
(?P<day>0?[1-9]|[12][0-9]|3[01]) # day: 1-31 with op onal 0
-
(?P<year>\d{4}) # year: 4 digits
(?P<a er>.*?)$ # all text a er the date
""", re.VERBOSE)

# --- Process files ---


for filename in os.listdir(folder_path):
match = date_pa ern.search(filename)
if not match:
con nue # Skip files without an American-style date

# 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")

# Build new filename in European format


european_filename = f"{before}{day}-{month}-{year}{a er}"

# Full paths
old_path = os.path.join(folder_path, filename)
new_path = os.path.join(folder_path, european_filename)

# Show the opera on


print(f"Renaming: {filename} -> {european_filename}")

# Perform the rename (only if dry_run is False)


if not dry_run:
shu l.move(old_path, new_path)
Output
Before

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

This Python program creates a backup of a folder by compressing its


contents into a .zip file.
1. It defines a func on called backup_folder that takes the name of
a folder as input.
2. Inside the func on:
o It gets the absolute path of the folder.
o It creates a .zip file with the same name as the folder (e.g.,
backing up example_folder will create example_folder.zip).
3. It then goes through all files inside the folder (including
subfolders) using os.walk().
4. For each file it finds, it adds that file to the .zip file using the zipfile
5. Finally, it runs this backup process for two folders:
example_folder and xyz_folder.

You might also like