0% found this document useful (0 votes)
15 views14 pages

Python File Management with shutil

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Python File Management with shutil

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link](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 [Link]( ) works is shown below:

 The first [Link]( ) call copies the file at C:\[Link] 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
[Link] filename is used for the new, copied file’s filename.
 The second [Link]( ) call 2, also copies the file at C:\[Link] to the folder
C:\delicious but the name given to the copied file is [Link].
 While [Link]( ) will copy a single file, [Link]( ) 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, [Link] 1
Module-4
 Calling [Link](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 [Link]( ) call creates a new folder named bacon_backup with the
same content as the original bacon folder.
Moving and Renaming Files and Folders
 Calling [Link](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
[Link]( ) calls says, “Move C:\[Link] into the folder C:\eggs.”
 If there had been a [Link] 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, [Link] 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:\[Link] into the folder C:\eggs, and while you’re at it,
rename that [Link] 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 [Link] 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 [Link] 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, [Link] 1
Module-4
Enter the following into the interactive shell:

>>> [Link]('[Link]', '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 [Link] 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 [Link](path) will delete the file at path.
2. Calling [Link](path) will delete the folder at path. This folder must be
empty of any files or folders.
3. Calling [Link](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, [Link] 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 [Link]( ) 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 [Link]( ) 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, [Link] 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, [Link] 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, [Link] 1
Module-4
 A single string value: the path of a folder is passed to the [Link]( ) function

 You can use [Link]( ) 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 [Link]( ) 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 [Link]( ).

 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 [Link]( ) 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, [Link] 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 [Link] that
has the contents shown in Figure 3.2.

Figure 3.2: The contents of [Link]

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 [Link]( ) 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, [Link] 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 [Link] 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, [Link] 1
Module-4

 After running this code, the contents of [Link] 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 [Link]('C:\\ delicious'),
the code would extract the files from [Link] 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, [Link] 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 [Link] that has the compressed
contents of [Link].
 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 [Link]( ) to open the ZIP file in append mode.

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

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, [Link] 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 [Link](dirName):
print("Directory", dirName, "doesn't exist")
[Link](0)
curDirectory = [Link](dirName)
with [Link]("[Link]", mode="w") as archive:
for file_path in [Link]("*"):
[Link](file_path, arcname=file_path.relative_to(curDirectory))
if [Link]("[Link]"):
print("Archive", "[Link]", "created successfully")
else:
print("Error in creating zip archive")
--------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
Enter Directory name that you want to backup: c:\a\b
Archive [Link] 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, [Link] 1
Module-4
Debugging

Prof. Poral Nagaraj & Prof. Avinash G M, Asst. Prof., Dept. of CS&E, SJMIT, [Link] 1

Common questions

Powered by AI

os.unlink deletes files immediately and permanently, removing them from the file system with no recovery options . In contrast, send2trash sends files and folders to the trash or recycle bin, enabling file recovery if they were incorrectly deleted . This makes send2trash safer when testing or deploying code where file integrity is critical .

To create a ZIP file using the zipfile module, you initialize a ZipFile object in write mode with the 'w' argument. Then, use the write method to add files, specifying the filename and the compression algorithm, usually zipfile.ZIP_DEFLATED. Appending to an existing ZIP uses the 'a' mode . Considerations include ensuring paths are correct and that the ZIP creation doesn't inadvertently overwrite important existing data .

ZIP files offer compressed storage, reducing file sizes and making data transfer more efficient over the Internet. They allow multiple files and directories to be packaged into a single archive file, simplifying file management and sharing, such as attaching multiple files in a single email . Extracting and managing the contents is straightforward with Python's zipfile module .

The send2trash module sends files and folders to the trash or recycle bin, allowing for recovery after accidental deletion, which is not possible with shutil.rmtree that permanently deletes them . This adds a layer of safety, especially during debugging or when running new or untrusted code, as it prevents irreversible data loss .

When the destination is a folder, shutil.copy retains the original filename of the source file and places the file into the specified folder, returning the path of the copied file as a string . If the destination is a filename, the source file is copied and given the new destination name, again returning the path of the new file .

The os.walk function helps iterate over directory contents by walking the directory tree starting from a given path. In each loop iteration, it yields three outputs: a string representing the current folder's name, a list of strings of the folders in the current folder, and a list of strings of the files in the current folder . Unlike the range function, os.walk does not change the current working directory .

Using shutil.move can lead to accidental overwriting of files if a file with the same name already exists in the destination directory. It can also mistakenly rename a file if the destination folder does not exist and is interpreted as a filename . To mitigate these risks, it is essential to check if the destination folders exist beforehand and handle exceptions. Additionally, backups of important files should be maintained before moving files .

shutil.copytree is preferred when the task is to copy an entire directory, including all its files and subdirectories, to a new location. This function simplifies copying entire structures without manual iteration over directory contents, providing a more concise and error-free approach compared to using shutil.copy for each file individually .

The os.makedirs function allows the creation of directories recursively, ensuring all intermediate directories needed for a path are created. This is vital when using shutil.move or os.rename, which require the destination path to exist. Combining these functions enables more reliable file management operations where directory setups aren't pre-established, avoiding path errors .

The os module provides os.unlink for deleting a single file and os.rmdir for deleting an empty directory. For deleting a folder and all its content recursively, the shutil module provides shutil.rmtree . These functions irrevocably remove data, so caution must be exercised while using them, such as ensuring correct paths and that backup data exists elsewhere .

You might also like