System Scripting Lab 10

Organising Files & File I/O

Setup

mkdir lab10
cd lab10
idle3

Exercise 1: Create Folder and Files

import os
import shutil

# Delete project folder if exists
if os.path.exists("project"):
    shutil.rmtree("project")

# Create folder
os.mkdir("project")

# Create files
files = ["file1.txt", "file2.txt", "file3.txt"]
for f in files:
    open(f"project/{f}", "w").close()

# Write content to one file
with open("project/file1.txt", "w") as f:
    f.write("This is sentence one.\n")
    f.write("This is sentence two.\n")
    f.write("This is sentence three.\n")

# Print directories
print("Current Directory:", os.getcwd())
print("Project Folder Contents:", os.listdir("project"))

Exercise 2: Folder Copy Function

import os
import shutil

def copy_folder(source, destination):
    # Check source exists
    if not os.path.exists(source):
        print("Source folder does not exist")
        return

    # Remove destination if exists
    if os.path.exists(destination):
        shutil.rmtree(destination)

    # Copy folder
    shutil.copytree(source, destination)

    print("Copy successful!")

    # Output file names and sizes
    for file in os.listdir(destination):
        path = os.path.join(destination, file)
        if os.path.isfile(path):
            print(file, os.path.getsize(path), "bytes")

# User input
src = input("Enter source folder: ")
dest = input("Enter destination folder: ")

copy_folder(src, dest)

Exercise 3: List .txt Files in Directory Tree

import os

def find_txt_files(folder):
    if not os.path.exists(folder):
        print("Folder does not exist")
        return

    for root, dirs, files in os.walk(folder):
        for file in files:
            if file.endswith(".txt"):
                print(os.path.join(root, file))

# User input
folder = input("Enter folder path: ")
find_txt_files(folder)

Key Concept: os.walk() traverses directory tree.

Exercise 4: Zip and Backup Folder

import os
import shutil
import zipfile

def archive_folder(folder):
    if not os.path.exists(folder):
        print("Folder does not exist")
        return

    # Create zip file
    zip_name = folder + ".zip"
    shutil.make_archive(folder, 'zip', folder)

    # Store directory in home
    home = os.path.expanduser("~")
    store = os.path.join(home, "store")

    # Delete and recreate store folder
    if os.path.exists(store):
        shutil.rmtree(store)
    os.mkdir(store)

    # Create 4 backup copies
    for i in range(1, 5):
        shutil.copy(zip_name, os.path.join(store, f"backup{i}.zip"))

    print("Backups created")

    # Verify by listing contents of one archive
    os.chdir(store)
    with zipfile.ZipFile("backup1.zip", 'r') as z:
        print("Contents of backup1.zip:")
        z.printdir()

# User input
folder = input("Enter folder to archive: ")
archive_folder(folder)

Key Concepts:

Key Exam Tips