COMP7004 - Systems Scripting
Lecture 15: Organising Files
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
Objectives
Python provides the ability to organise pre-existing files on
a hard drive. This enables automating boring tasks such as:
Making copies of all PDF files (and only the PDF files) in
every sub-folder of a folder.
Removing the leading zeros in the filename for every file in
a folder of hundreds of files named as cat001.txt,
cat002.txt, cat003.txt, and so on.
Compressing the content of several folders into one ZIP file
(which could be a simple backup system).
2
Accessing User Home Directory
Python provides a function in the os module to return
a user home directory.
The function
os.path.exapanduser()
Passing tilda (~) as parameter to this function ensure
interoperability across platforms.
Example
3
Copying Files and Folders
The shutil (or shell utilities) module provides functions
for copying files, as well as entire folders.
To copy a file, we can call shutil.copy(source,
destination). This will copy the file at the path source
to the folder at the path destination. Both source and
destination are string data type.
If the destination is a filename, it will be used as the
new name of the copied file. This function returns a
string of the path of the copied file.
4
Copying Files and Folders
While shutil.copy() will copy a single file, shutil.copytree() will copy an
entire folder and every subfolders and files contained in it.
Calling shutil.copytree(source, destination) will copy the folder at the
path source, along with all of its files and subfolders, to the folder at
the path destination. The source and destination parameters are both
strings.
It returns a string of the path of the copied folder.
5
Moving and Renaming Files and
Folders
The shutil module provides the shutil.move() function for
this purpose.
Calling shutil.move(source, destination) will move the le or
folder at the path source to the path destination and will
return a string of the absolute path of the new location.
If destination points to a folder, the source le gets moved
into destination and keeps its current lename. See
following example.
If the lename exist in the destination folder, it will be
silently overwritten.
6
Moving and Renaming Files and
Folders
If the folder “backup” is not existing, the function will
rename the file tips.txtinto “backup” at the path
destination. Be careful in using this function.
The destination path can also specify a filename. In this
case, the source filename will be renamed to the name
specified at the destination path.
7
Permanently Deleting Files and
Folders
One can delete a single file or a single empty folder with
functions in the os module, whereas for a non-empty folder
and all of its contents, we use the shutil module.
Calling os.unlink(path) will delete a file at the specified path.
Calling os.rmdir(path) will delete a folder at the specified path. The
folder must be empty of any files or folders.
Calling shutil.rmtree(path) will remove the folder at the specified
path and all files and folders it contains will be deleted too.
Be very careful in using these functions. It is a good idea to first
print out the content intended to be deleted before calling
these delete function on the content to avoid mistakenly
deleting important files.
8
Deleting Files: Example
This will permanently delete all the files in the current
working directory that has the ending .rxt”.
Always print out the file names first to know exactly the
files you are about to delete.
9
Safe Deletes with the send2trash
Module
Since Pythons built-in shutil.rmtree() function irreversibly
deletes files and folders, it can be dangerous to use.
A much better way to delete files and folders is with the third-
party send2trash module.
The send2trash module can be installed from a Terminal
window by simply running python3 m pip install send2trash.
Using send2trash is much safer because it will send folders
and files to your computer’s recycle bin or trash instead of
permanently deleting them.
10
send2trash Module: Example
While sending files to the recycle bin lets you recover them
later, it will not free up disk space like permanently deleting
them does. If you want your program to free up disk space, use
the os and shutil functions to delete file and folders.
Note that the send2trash() function can only send files to the
recycle bin, it cannot pull files out of it.
11
Walking a Directory Tree
Let assume you want to rename every file in some folder
and also every file in every subfolder of that folder. That is,
you want to walk through the directory tree touching each
file as you go.
Python provides the os.walk() function to handle this
process for you. This function accepts a single value the
path of a folder.
You can use os.walk() in a for loop statement to walk a
directory tree, much like how you can use the range()
function to walk over a range of numbers.
12
Walking a Directory Tree
Unlike range(), the os.walk() function will return three
values on each iteration through the loop:
1. A string of the current folders name.
2. A list of strings of subfolders in the current folder
3. A list of strings of the files in the current folder
Note that the current folder means the folder for the
current iteration of the for loop. The current working
directory of the program is not changed by os.walk()
function.
See the following example.
13
Walking a Directory Tree: Example
Just like you can choose the variable name ’iin a for loop,
you can choose variable names for the three values.
One can use a loop to iterate over the three variables.
14
Walking a Directory Tree: Example
Output
15
Compressing Files with the zipfile
Module
Compressing a file reduces its size, which is useful for
transferring it over the Internet.
A Zip file can contain multiple files and subfolders. So
it is a handy way to package several files into one
archive.
Python can both create and open (or extract) Zip files
using functions in the zipfile module.
16
Reading ZIP Files
To read the content of a ZIP le, you must rst create a
ZipFile object (note the capital letters Z and F).
ZipFile objects are conceptually similar to the File object
we saw returned by the open() function previously. They
are values through which the program interacts with the
le.
To create a ZipFile object, call ziple.ZipFile() function,
passing it a string of the .zip les lename.
Note that ziple is the name of the Python module, and
ZipFile() is the name of the function.
17
Reading ZIP Files: Example
18
Creating and Adding to ZIP Files
To create your own compressed ZIP files, you must open the ZipFile
object in write mode by passing w as the second argument.
When you pass a path to the write() method of a ZipFile object, Python
will compress the file at that path and add it into the ZIP file.
The write() methods first argument is a string of the filename to add.
The second argument is the compression type parameter, which tell
the computer what algorithm it should use to compress the file. You
can just set this value to zipfile.ZIP_DEFLATED.
See the following example:
19
Creating and Adding to ZIP Files
Keep in mind that, just as with writing to les, write mode will erase all
existing content of a ZIPle.
If you want to add le to an existing ZIPle, pass aas the second
argument to ziple.ZipFile() to open the ZIPle in append mode.
Output showing created ZIP file
20
Extracting from ZIP Files
Calling the extractall() method on ZipFile objects extracts all the files
and folders from a ZIP file into the current working directory.
Example:
Optionally, you can pass a folder name as an argument to extractall()
to have it extract the files in a folder other than the current working
directory. If the folder name passed does not exist, it will create it.
21
Extracting from ZIP Files
The extract() method for ZipFile object will extract a single file
from the ZIP file.
Let us continue with the previous example:
The string you pass to extract() must match one of the strings
in the list returned by namelist(). You can optionally, pass a
second argument to extract() to extract to a folder other than
the current working directory. Python will create the folder if it
does not exist.
22
Use Case Scenario: Backing Up a
Folder
Say you are working on a project whose files you keep in a folder
name /home/vin/progProject. You are worried about loosing your
work, so you would like to create ZIP file snapshots of the entire
folder.
You would like to keep different versions, so you want the ZIP files
filename to increment each time it is made.
For example:
progProject_1.zip, progProject_2.zip, progProject_3.zip and so on.
You could do this by hand but it is rather annoying and you might
accidentally misnumber the ZIP files’ names. It would be much
simpler to run a program that does this boring task for you.
23
Step1: Figure out the ZIP File’s
Name
The whole program will be written as a Python function so that
it will be easy to reuse it in different Python programs.
We will create a function that takes one parameter as
argument. The parameter will represent the path to the folder
name.
To name the ZIP file, we will extract the base name from the
folder path. We can then concatenate it with a running number
count.
Before a new folder with incremented count number is created,
we have to first check whether it already exist using the
os.path.exists() function.
24
Working Solution Structure
25
Step2: Create the New ZIP File
Now that the new ZIP files name is stored in the
zipFilename variable, you can call zipfile.ZipFile()
function to actually create the ZIP file.
Be sure to pass w’ as second argument so that the
ZIP file is opened in write mode.
26
Step3: Walk the Directory Tree and
Add to ZIP File
You can use os.walk() function in a for loop to iterate
over the directories.
27
Complete Solution
28
Thank You!
29