0% found this document useful (0 votes)
9 views47 pages

Python File Operations and Automation

This document provides an overview of organizing files in Python using modules such as shutil, os, and zipfile. It covers file operations like copying, moving, deleting, and compressing files, along with examples and key functions for each module. Additionally, it includes projects for renaming files and backing up folders, as well as a section on debugging and logging in Python.

Uploaded by

Rumana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views47 pages

Python File Operations and Automation

This document provides an overview of organizing files in Python using modules such as shutil, os, and zipfile. It covers file operations like copying, moving, deleting, and compressing files, along with examples and key functions for each module. Additionally, it includes projects for renaming files and backing up folders, as well as a section on debugging and logging in Python.

Uploaded by

Rumana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

MODULE 4

Organizing Files

Rumana Anjum
Dept of CSE
VVIET, MYSORE
Overview

Organizing Files
Learn how to automate file and folder operations like copying, moving,
renaming, and compressing using Python’s built-in modules such as:

● shutil – for high-level file operations (copy, move, delete).


● os – for navigating directories and working with paths.
● zipfile – for compressing and extracting files in .zip format.
Organizing Files: The shutil Module
The shutil (or shell utilities) module has functions to let you copy, move, rename, and
delete files in your Python programs
Copying Files and Folders
The shutil module provides functions for copying files, as well as entire folders.
Calling [Link](source, destination) will copy the file at the path source to the
folder at the path destination.
>>> import shutil, os
>>> [Link]('C:\\')
>>> [Link]('C:\\[Link]', 'C:\\delicious') 'C:\\delicious\\[Link]'
>>> [Link]('[Link]', 'C:\\delicious\\[Link]') 'C:\\delicious\\[Link]'
Moving and Renaming Files and Folders
>>> import shutil
>>> [Link]('C:\\[Link]', 'C:\\eggs') 'C:\\eggs\\[Link]'
Key Functions in shutil

Function Description
[Link](src, dst) Copies file from source to
destination.

shutil.copy2(src, dst) Same as copy() but preserves


metadata.

[Link](src, dst) Moves file from source to


destination (can rename too).

[Link](path) Recursively deletes a directory and


its contents.

[Link](src, dst) Copies an entire folder structure.


Permanently Deleting Files and Folders
You can delete a single file or a single empty folder with functions in the os module, whereas to delete a folder and
all of its contents, you use the shutil module.

 Calling [Link](path) will delete the file at path.

 Calling [Link](path) will delete the folder at path. This folder must be empty of any files or folders

 Calling [Link](path) will remove the folder at path, and all files and folders it contains will also be deleted.

import os

for filename in [Link]():

if [Link]('.rxt'):

[Link](filename)

Safe Deletes with the send2trash Module >>> import send2trash


Using send2trash is much safer than Python’s >>> baconFile = open('[Link]', 'a') # creates the file

Regular delete functions, because it will send >>> [Link]('Bacon is not a vegetable.')

folders and file to your computer’s trash or 25

recycle bin instead of permanently deleting >>> [Link]()

them. >>> send2trash.send2trash('[Link]')


Walking a Directory Tree

Python’s os module allows you to interact with the operating system, including directories and file
paths.
One of the most powerful functions in this module is [Link](),
which helps you traverse through all subfolders and files in a directory tree.
What is [Link]()?
[Link](directory) generates the file names in a directory tree by
walking top-down or bottom-up.
It yields a 3-tuple:
○ foldername: The name of the current folder.
○ subfolders: A list of subfolder names in the current folder.
○ filenames: A list of file names in the current folder.
Walking a Directory Tree

import os
for foldername, subfolders, filenames in [Link]('my_project'):
print('Current folder:', foldername)
for subfolder in subfolders:
print(' Subfolder:', subfolder)
for filename in filenames:
print(' File:', filename)

OUTPUT

Current folder: my_project


Subfolder: docs
Subfolder: images
File: [Link]
Compressing Files with the zipfile Module

Python provides the zipfile module to work with .zip files.


It allows you to create, read, write, append, and extract ZIP archives
without using external tools like WinRAR or 7-Zip.
Why Use ZIP Files in Python?
● Efficient storage of multiple files/folders in a single archive.
● Useful for creating backups, sharing projects, and automated deployments.
● Helps save space and organize large sets of files.
Key Methods in [Link]
Method Description

write(filename) Add a file to the ZIP archive.

extractall(path) Extract all files into the specified


folder.

namelist() Returns list of file names in the


archive.
Compressing Files with the zipfile Module

Example: Extracting a ZIP File


Creating a ZIP File
import zipfile

import zipfile with [Link]('[Link]', 'r') as archive:

with [Link]('[Link]', 'w') as archive: [Link]('extracted_files')


● All contents will be extracted to a folder called
[Link]('[Link]')
extracted_files.
[Link]('[Link]')
Example: Listing Files in ZIP
with [Link]('[Link]', 'r') as archive:
 'w' means write mode – this creates a new print([Link]())

archive. Caution:
 You can also use 'a' (append) or 'r' (read- ● zipfile only handles .zip files (not .rar, .tar, etc.).
● Always check if a file exists before writing or
only). extracting to avoid overwrites.
Project – Renaming Files from American to European
Dates

Build a Python script that renames files in a folder from the American date format (MM-DD-YYYY) to the
European date format (DD-MM-YYYY).
Real-World Use Case:
● Standardize naming of documents across international teams.
● Automatically convert date-based filenames when sharing between US and EU teams.

import os
import re

date_pattern = [Link](r'(\d{2})-(\d{2})-(\d{4})') # MM-DD-YYYY How it Works:

for filename in [Link]('.'): ● It looks for filenames containing dates in


mo = date_pattern.search(filename) MM-DD-YYYY format.
if mo: ● Extracts the month, day, and year.
mm, dd, yyyy = [Link]() ● Rearranges them into DD-MM-YYYY
european_format = f"{dd}-{mm}-{yyyy}" format.
new_name = date_pattern.sub(european_format, filename) ● Renames the file using [Link]().
[Link](filename, new_name)
print(f'Renamed: {filename} → {new_name}')
Project – Backing Up a Folder to
ZIP File
import zipfile, os
How It Works: def backup_folder(folder_path):
folder_path = [Link](folder_path)
1. Converts the folder path to an
zip_name = [Link](folder_path) + '_backup.zip'
absolute path.
print(f"Creating backup as: {zip_name}")
2. Walks through the folder using with [Link](zip_name, 'w') as backup_zip:

[Link]().
for foldername, subfolders, filenames in [Link](folder_path):
for filename in filenames:
3. Adds every file found to a ZIP
file_path = [Link](foldername, filename)
archive using [Link](). backup_zip.write(file_path, [Link](file_path,
4. Uses [Link]() to folder_path))
print("Backup complete!")
maintain folder structure inside
backup_folder('my_project') # Change 'my_project' to your folder
the ZIP.
CHAPTER 2 - DEBUGGING
Raising Exceptions:

Python raises an exception whenever it tries to execute invalid code.

Exceptions are raised with a raise statement. In code, a raise statement consists of the following:

➢The raise keyword.

➢A call to the Exception() function.

➢A string with a helpful error message passed to the Exception() function.


Example:
Example:
def divide(a, b):
if b == 0:
raise ValueError("Denominator cannot be zero!") # Raise
an exception
return a / b

try:
result = divide(10, 0) # This will raise an exception
print("Result:", result) OUTPUT:
except ValueError as e: Caught an exception:
Denominator cannot be zero!
print("Caught an exception:", e)
Getting the Traceback as a String:
 When Python encounters an error, it produces a treasure trove of error
information called the traceback.
The traceback includes
 The error message,
 The line number of the line that caused the error, and
 The sequence of the function calls that led to the error.
Output:
Traceback (most recent call last):
File "C:/Users/HP/Desktop/PYTHON/[Link]", line 5, in <module>
spam()
File "C:/Users/HP/Desktop/PYTHON/[Link]", line 2, in spam
bacon()
File "C:/Users/HP/Desktop/PYTHON/[Link]", line 4, in bacon
raise Exception('This is error message')
Exception: This is error message
 We can also obtain it as a string by calling
traceback.format_exc().
 This function is useful if you want the information from an
exception’s traceback but also want an except statement to
gracefully handle the [Link]:
Output:
Assertions:

 An assertion is a sanity check to make sure your code isn’t doing


something obviously wrong.
 If the sanity check fails, then an AssertionError exception is raised.
Assert statement consists of the following:
The assert keyword
 A condition (that is, an expression that evaluates to True or False)
 A comma
 A string to display when the condition is False
Example:
def divide(a, b):
assert b != 0, "Denominator must not be
zero"
return a / b
print(divide(10, 2)) # Works
print(divide(5, 0)) # Raises AssertionError
Output:
Using an Assertion in a Traffic Light Simulation:

 The data structure representing the stoplights at an intersection is a

dictionary with keys 'ns' and 'ew', for the stoplights facing north-south

and east-west, respectively.

 The values at these keys will be one of the strings 'green', 'yellow', or

'red'. The code would look something like this:

market_2nd = {'ns': 'green', 'ew': 'red'}

mission_16th = {'ns': 'red', 'ew': 'green'}


Define traffic light states:
trafficLights = {
'ns': 'green', # north-south direction is green
'ew': 'red' # east-west direction is red
}
 TrafficLights is a dictionary representing the current state of
the traffic lights.

 'ns' stands for north-south direction, initially green.

 'ew' stands for east-west direction, initially red.

 This setup means cars can go north-south, but must stop east-
west.
Define a function:

def switchLights(intersection)
 This defines a function named switchLights.

 It takes one argument intersection, which will be a dictionary like


trafficLights.

Change the light:

if intersection['ns'] == 'green':

intersection['ns'] = 'yellow'

intersection['ew'] = 'red'
 If north-south light is green, change it to yellow.

 East-west remains red (cars still wait).


elif intersection['ns'] == 'yellow':
intersection['ns'] = 'red'
intersection['ew'] = 'green'
 If north-south is yellow, turn it to red (stop).
 Now east-west turns green (they can go).
Similarly:
elif intersection['ns'] == 'red':
intersection['ns'] = 'green'
intersection['ew'] = 'red'

if intersection['ns'] == 'green':
intersection['ns'] = 'yellow'
intersection['ew'] = 'red'
Assert valid state:
assert 'red' in [Link](), 'Neither light is red! ' + str(intersection)
 This line checks: at least one light must be red.
 If not, the program crashes with an AssertionError.
 This helps detect bugs if both directions mistakenly become green (which is unsafe in real life!).

Simulate and print:


print("Before:", trafficLights)
switchLights(trafficLights)
print("After 1st switch:", trafficLights)
switchLights(trafficLights)
print("After 2nd switch:", trafficLights)
switchLights(trafficLights)
print("After 3rd switch:", trafficLights)
Sample output:

Before: {'ns': 'green', 'ew': 'red'}


After 1st switch: {'ns': 'yellow', 'ew': 'red'}
After 2nd switch: {'ns': 'red', 'ew': 'green'}
After 3rd switch: {'ns': 'green', 'ew': 'red'}
Logging:
 Logging is a great way to understand what’s happening in
your program and in what order its happening.
 Python’s logging module makes it easy to create a record of
custom messages that you write.
 Logging levels provide a way to categorize your log messages
by importance. There are five logging levels.
import logging
[Link](level=[Link], format=' %
(asctime)s -
%(levelname)s - %(message)s')
Output:

Sample logs:
Sample log for factorial program (For your reference):
2015-05-23 [Link],650 - DEBUG - Start of program 2015-05-23
[Link],651 - DEBUG - Start of factorial(5) 2015-05-23 [Link],651 -
DEBUG - i is 1, total is 1

2015-05-23 [Link],654 - DEBUG - i is 2, total is 2

2015-05-23 [Link],656 - DEBUG - i is 3, total is 6

2015-05-23 [Link],659 - DEBUG - i is 4, total is 24

2015-05-23 [Link],661 - DEBUG - i is 5, total is 120

2015-05-23 [Link],661 - DEBUG - End of factorial(5) 120

2015-05-23 [Link],666 - DEBUG - End of program


Disabling Logging:

 The [Link]() function disables these so that you don’t


have to go into your program and remove all the logging calls
by hand.

 Pass [Link]() a logging level, and it will suppress all


log messages at that level or lower
Example:
import logging
[Link](level=[Link])
[Link]("This will show")
[Link]("This will show") Output:

# Disable logging
[Link]([Link])
[Link]("This will NOT show")
[Link]("This will NOT show")
[Link]("This will NOT show")
Logging to a File:
Instead of displaying the log messages to the screen,
you can write them to a text file. The
[Link]() function takes a filename keyword
argument.
Example:
IDLE ’s Debugger:

 The debugger is a feature of IDLE that allows you to execute


your program one line at a time.

 Debugger will pause execution before the first instruction


and display the following:
 The line of code that is about to be executed
 A list of all local variables and their values
 A list of all global variables and their values
The Debug Control window
The program will stay paused until you press one of the five
buttons in the Debug Control window: Go, Step, Over, Out, or Quit.
Go:
Clicking the Go button will cause the program to execute normally
until it terminates or reaches a breakpoint

Step:
 Clicking the Step button will cause the debugger to execute the
next line of code and then pause again.
 If the next line of code is a function call, the debugger will “step
into” that function and jump to the first line of code of that
function.
Over
The Over button will “step over” the code in the function. The
function’s code will be executed at full speed, and the debugger
will pause as soon as the function call returns.

Quit
If you want to stop debugging entirely and not bother to
continue executing the rest of the program, click the Quit button
Example: Debugging a Number Adding Program
The Debug Control window when the program first
starts under the debugger
NOTE: The Debug Control window on the
last line. The variables are set to strings,
causing the bug.
Breakpoints:
A breakpoint can be set on a specific line of code and
forces the debugger to pause whenever the program
execution reaches that line.
Example:
Output:
Halfway done!
Heads came up 490 times.

NOTE:

If you were interested in the value of heads at the halfway


point of the program’s execution, when 500 of 1000 coin flips
have been completed, you could instead just set a breakpoint
on the line print('Halfway done!')
THANK YOU

You might also like