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

Python ZIp Assignment

The document describes two Python programs: the first renames files from American-style dates (MM-DD-YYYY) to European-style dates (DD-MM-YYYY) using regex, while the second creates a backup of a folder by compressing its contents into a .zip file. The first program processes files in a specified folder, extracts date components, constructs new filenames, and renames them if not in dry run mode. The second program defines a function to backup a folder, iterating through its contents and adding them to a .zip file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Python ZIp Assignment

The document describes two Python programs: the first renames files from American-style dates (MM-DD-YYYY) to European-style dates (DD-MM-YYYY) using regex, while the second creates a backup of a folder by compressing its contents into a .zip file. The first program processes files in a specified folder, extracts date components, constructs new filenames, and renames them if not in dry run mode. The second program defines a function to backup a folder, iterating through its contents and adding them to a .zip file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, 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 shutil

# --- CONFIGURATION ---


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

# --- Regex with named groups ---


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

# --- Process files ---


for filename in os.listdir(folder_path):
match = date_pattern.search(filename)
if not match:
continue # 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")
after = match.group("after")

# Build new filename in European format


european_filename = f"{before}{day}-{month}-{year}{after}"

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

# Show the operation


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

# Perform the rename (only if dry_run is False)


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

After
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 pattern 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
pattern.
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 operation (old filename → new
filename).
9. If dry_run is set to False, actually renames the files on disk using
shutil.move.
10. Otherwise, if dry_run is True, it only simulates the rename
by printing.
Program-2 Backing up a folder into a zip file.
Main program

Output
Before Executing
After Executing

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


contents into a .zip file.
1. It defines a function called backup_folder that takes the name
of a folder as input.
2. Inside the function:
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