0% found this document useful (0 votes)
10 views

Module-4 Notes (1)

The document provides an overview of the shutil and zipfile modules in Python, detailing functions for organizing files such as copying, moving, renaming, and deleting files and folders. It also introduces the send2trash module for safer deletion and explains how to walk through a directory tree and compress files into ZIP archives. Additionally, it includes a sample program for backing up a folder into a ZIP file.

Uploaded by

ksomesha13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module-4 Notes (1)

The document provides an overview of the shutil and zipfile modules in Python, detailing functions for organizing files such as copying, moving, renaming, and deleting files and folders. It also introduces the send2trash module for safer deletion and explains how to walk through a directory tree and compress files into ZIP archives. Additionally, it includes a sample program for backing up a folder into a ZIP file.

Uploaded by

ksomesha13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Module-4

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.
 To use the shutil functions, you will first need to use import shutil.

Copying Files and Folders


 The shutil module provides functions for copying files, as well as entire folders.
 Calling shutil.copy(source, destination) will copy the file at the path source to
the folder at the path destination. (Both source and destinations are strings).
 If 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.
 How shutil.copy( ) works is shown below:

 The first shutil.copy( ) call copies the file at C:\spam.txt to the folder
C:\delicious.
 The return value is the path of the newly copied file.
 Note that since a folder was specified as the destination 1, the original
spam.txt filename is used for the new, copied file’s filename.
 The second shutil.copy( ) call 2, also copies the file at C:\eggs.txt to the folder
C:\delicious but the name given to the copied file is eggs2.txt.
 While shutil.copy( ) will copy a single file, shutil.copytree( ) will copy an entire
folder and every folder and file contained in it.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 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.
 The function returns a string of the path of the copied folder.
Example:

 The shutil.copytree( ) call creates a new folder named bacon_backup with the
same content as the original bacon folder.
Moving and Renaming Files and Folders
 Calling shutil.move(source, destination) will move the file 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 file gets moved into destination
and keeps its current filename.
For example,

 Assuming a folder named eggs already exists in the C:\ directory, this
shutil.move( ) calls says, “Move C:\bacon.txt into the folder C:\eggs.”
 If there had been a bacon.txt file already in C:\eggs, it would have been
overwritten.
 Since it’s easy to accidentally overwrite files in this way, you should take
some care when using move( ).

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 The destination path can also specify a filename.
 In the following example, the source file is moved and renamed.

 This line says, “Move C:\bacon.txt into the folder C:\eggs, and while you’re at it,
rename that bacon.txt file to new_bacon.txt.”

 Both of the previous examples worked under the assumption that there was a
folder eggs in the C:\ directory. But if there is no eggs folder, then move( ) will
rename bacon.txt to a file named eggs.

 Here, move( ) can’t find a folder named eggs in the C:\ directory and so
assumes that destination must be specifying a filename, not a folder.
 So the bacon.txt text file is renamed to eggs (a text file without the .txt
file extension).

 This can be a tough-to-spot bug in your programs since the move( ) call can
happily do something that might be quite different from what you were
expecting.
 This is yet another reason to be careful when using move( ).

 Finally, the folders that make up the destination must already exist, or else
Python will throw an exception.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
Enter the following into the interactive shell:

>>> shutil.move('spam.txt', 'c:\\does_not_exist\\eggs\\ham')

 Python looks for eggs and ham inside the directory does_not_exist.
 It doesn’t find the nonexistent directory, so it can’t move spam.txt to the
path you specified.
----------------------------------------------------------------------------------------------------
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.
1. Calling os.unlink(path) will delete the file at path.
2. Calling os.rmdir(path) will delete the folder at path. This folder must be
empty of any files or folders.
3. Calling shutil.rmtree(path) will remove the folder at path, and all files and
folders it contains will also be deleted.
 Be careful when using these functions in your programs!

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 Here is a Python program that was intended to delete files that have the .txt file
extension but has a typo (highlighted in bold) that causes it to delete .rxt files
instead:

 If you had any important files ending with .rxt, they would have been
accidentally, permanently deleted.
 Instead, you should have first run the program like this:

 Now the os.unlink( ) call is commented, so Python ignores it.


 Instead, you will print the filename of the file that would have been
deleted.
 Running this version of the program first will show you that you’ve accidentally
told the program to delete .rxt files instead of .txt files.
-------------------------------------------------------------------------------------------------------------------

Safe Deletes with the send2trash Module


 Since Python’s 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.
 You can install this module by running pip install send2trash from a
Terminal window.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 Using send2trash is much safer than Python’s regular delete functions,
because it will send folders and files to your computer’s trash or recycle bin
instead of permanently deleting them.
 If a bug in your program deletes something with send2trash, you didn’t intend
to delete, you can later restore it from the recycle bin.
Example:

 In general, you should always use the send2trash.send2trash( ) function to


delete files and folders.
 But 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 for deleting files and folders. Note that the send2trash( ) function can
only send files to the recycle bin; it cannot pull files out of it.
---------------------------------------------------------------------------------------------------------------------

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
Walking a Directory Tree
 Roughly 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.
 Writing a program to do this could get tricky; fortunately, Python provides a
function to handle this process for you.

Let’s look at the C:\delicious folder with its contents, shown in Figure 3.4

Figure 3.4: An example folder that contains three folders and four files

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 A single string value: the path of a folder is passed to the os.walk( ) function

 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.
 Unlike range( ), the os.walk( ) function will return three values on each
iteration through the loop:
1. A string of the current folder’s name
2. A list of strings of the folders in the current folder
3. A list of strings of the files in the current folder

Note: By 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( ).

 Just like you can choose the variable name i in the code for i in range(10):, you
can also choose the variable names for the three values listed earlier. We
usually use the names folder name, subfolders, and filenames.
When you run this program, it will output the following:

 Since os.walk( ) returns lists of strings for the subfolder and filename
variables, you can use these lists in their own for loops.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 Replace the print( ) function calls with your own custom code. (Or if you don’t
need one or both of them, remove the for loops.)

Compressing Files with the zipfile Module

 You may be familiar with ZIP files (with the .zip file extension), which can hold
the compressed contents of many other files.
 Compressing a file reduces its size, which is useful when transferring it
over the Internet.
 And since a ZIP file can also contain multiple files and subfolders, it’s a handy
way to package several files into one.
 This single file, called an archive file, can then be, say, attached to an
email.
 Your Python programs can both create and open (or extract) ZIP files using
functions in the zipfile module. Say you have a ZIP file named example.zip that
has the contents shown in Figure 3.2.

Figure 3.2: The contents of example.zip

Reading ZIP Files


 To read the contents of a ZIP file, first you must create a ZipFile object (note the
capital letters Z and F).
 ZipFile objects are conceptually similar to the File objects.
 To create a ZipFile object, call the zipfile.ZipFile( ) function, passing it a string
of the .zip file’s filename.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
 Note that zipfile is the name of the Python module, and ZipFile( ) is the name of
the function.

 A ZipFile object has a namelist( ) method that returns a list of strings for all
the files and folders contained in the ZIP file.
 These strings can be passed to the getinfo( ) ZipFile method to return a ZipInfo
object about that particular file.
 ZipInfo objects have their own attributes, such as file_size and compress_size in
bytes, which hold integers of the original file size and compressed file size,
respectively.
 While a ZipFile object represents an entire archive file, a ZipInfo object holds
useful information about a single file in the archive.
 The command at 1 calculates how efficiently example.zip is compressed by
dividing the original file size by the compressed file size and prints this
information using a string formatted with %s.
--------------------------------------------------------------------------------------------------------
Extracting from ZIP Files
 The extractall( ) method for ZipFile objects extracts all the files and folders
from a ZIP file into the current working directory.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4

 After running this code, the contents of example.zip will be extracted to C:\.
 Optionally, you can pass a folder name to extractall( ) to have it extract the
files into a folder other than the current working directory.
 If the folder passed to the extractall( ) method does not exist, it will be
created.
Example:
 If you replaced the call at 1 with exampleZip.extractall('C:\\ delicious'),
the code would extract the files from example.zip into a newly created
C:\delicious folder.
 The extract( ) method for ZipFile objects will extract a single file from the ZIP
file.

 The string you pass to extract( ) must match one of the strings in the list
returned by namelist( ).
 Optionally, you can pass a second argument to extract( ) to extract the file
into a folder other than the current working directory.
 If this second argument is a folder that doesn’t yet exist, Python will
create the folder.
 The value that extract( ) returns is the absolute path to which the file was
extracted.

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
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( ) method’s first argument is a string of the filename to add.
 The second argument is the compression type parameter, which tells
the computer what algorithm it should use to compress the files; you
can always just set this value to zipfile.ZIP_DEFLATED.

Example:

 This code will create a new ZIP file named new.zip that has the compressed
contents of spam.txt.
 Keep in mind that, just as with writing to files, write mode will erase all existing
contents of a ZIP file.
 If you want to simply add files to an existing ZIP file, pass 'a' as the second
argument to zipfile.ZipFile( ) to open the ZIP file in append mode.

-----------------------------------------------------------------------------------------------------------

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
Example Program (Lab Program)

Develop a program to backing up a given Folder (Folder in a current working


directory) into a ZIP File by using relevant modules and suitable methods.

import os
import sys
import pathlib
import zipfile
dirName = input("Enter Directory name that you want to backup: ")
if not os.path.isdir(dirName):
print("Directory", dirName, "doesn't exist")
sys.exit(0)
curDirectory = pathlib.Path(dirName)
with zipfile.ZipFile("myZip.zip", mode="w") as archive:
for file_path in curDirectory.rglob("*"):
archive.write(file_path, arcname=file_path.relative_to(curDirectory))
if os.path.isfile("myZip.zip"):
print("Archive", "myZip.zip", "created successfully")
else:
print("Error in creating zip archive")
--------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
Enter Directory name that you want to backup: c:\a\b
Archive myZip1.zip created successfully

Enter Directory name that you want to backup: c:\raj\noDirectory


Directory c:\raj\noDirectory doesn't exist

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1
Module-4
Debugging

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, Chitradurga.Page 1

You might also like