Python_Module4.1
Python_Module4.1
Organizing files
Topics
Organizing Files: The shutil module
copy, move, rename, delete
Compressing and zipping files
The shutil Module
import shutil
import os
os.chdir('D:\\')
h = shutil.copy('D:\\hello.txt', 'D:\\delicious') #
hello = D:\delicious\hello.txt
First Hello.txt file copied to Delicious folder
print('hello = ', h) hi = D:\delicious\hi.txt
Hello.txt file copied to Delicious folder as
if h: hi.txt
print('Hello.txt file copied to Delicious folder')
hi = shutil.copy('hello.txt', 'D:\\delicious\\hi.txt') #
Second
print('hi = ', hi)
if hi:
print('Hello.txt file copied to Delicious folder as
Try yourself………..
>>>import shutil
While shutil.copy() will copy a shutil.copytree(source, destination)
single file, shutil.copytree() will
copy an entire folder and every
folder and file 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 shutil.copytree() creates a new folder
The source and destination named del_backup with the same content as
the original delicious folder
parameters are both strings.
Example:
import shutil
import os D:\delicious\walnut\waffles
print('Current Working Directory', os.getcwd())
os.chdir('D:\\') C:\del_backup\walnut\waffles
if shutil.copytree('D:\\delicious', 'C:\\del_backup'):
print("del_backup folder and its contents are copied to
C Drive")
Current Working Directory C:\Users\Lenovo\PycharmProjects\
pythonProject
del_backup folder and its contents are copied to C Drive
Moving and Renaming Files and Folders
Calling shutil.move(source,
Example:
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.
Example:
import shutil
import os
os.chdir('C:\\')
ctoc = shutil.move('C:\\sample\\sss.txt', 'C:\\del_backup')
print('Path of sss.txt moved --- ', ctoc)
ctod = shutil.move('C:\\sample\\yyy.txt', 'D:\\delicious')
print('Path of yyy.txt moved --- ', ctod)
import os
for filename in os.listdir():
if filename.endswith('.txt'):
os.unlink(filename)
import os
for filename in os.listdir():
if filename.endswith('.txt'):
# os.unlink(filename)
print(filename)
Safe Deletes with the send2trash
Module
• Since 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.
• Install this module by running pip install send2trash from a Terminal window. (See
Appendix A for a more in-depth explanation of how to install third-party
modules.)
• Using send2trash is safer than Python’s delete functions, because it sends folders
and files to the computer’s trash or recycle bin instead of permanently deleting
them.
• If a bug in the ,program deletes something with send2trash, that was not
intended to delete, it can be later restored from the recycle bin
Walking a Directory Tree – os.walk()
• Python provides a function to handle the process to walk through the
directory tree, looking each file as traversed for example to rename every
file in some folder and also every file in every subfolder of that folder.
• The os.walk() function is passed a single string value: the path of a folder.
• os.walk() is used in a for loop statement to walk a directory tree.
• The os.walk() function returns 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
import os
for folderName, subfolders, filenames in
os.walk('D:\\delicious'):
print('The current folder is ' + folderName)
for subfolder in subfolders:
print('SUBFOLDER OF ' + folderName + ': ' +
subfolder)
for filename in filenames:
print('FILE INSIDE ' + folderName + ': ' +
filename)
print('')
• Since os.walk() returns lists of strings for the subfolder and filename
variables, use these lists in their own for loops.
• 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
• 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 used to send or
store thru email as attachment
• Python programs can create and open (or extract) ZIP files using
functions in the zipfile module.
Reading ZIP Files
• To read the contents of a ZIP file, first create a ZipFile object (note
the capital letters Z and F).
• ZipFile objects are similar to the File objects returned by the
open() function, which are values through which the program
interacts with the file.
• zipfile.ZipFile() function, is used to create a ZipFile object,
passing it a string of the .zip file’s filename.
• zipfile is the name of the Python module, and ZipFile() is the
name of the function.
• namelist() function extracts the file names into a list assigned
Example
Program for Reading ZIP File contents
import zipfile
import os
os.chdir('D:\\') # move to the folder with northcampus.zip
exampleZip = zipfile.ZipFile('northcampus.zip')
print('northcampus.zip namelist : ', exampleZip.namelist())
filenames = exampleZip.namelist() # List of filenames extracted using namelist function
helloInfo = exampleZip.getinfo(filenames[1])
print('Information of ' + filenames[1], helloInfo)
print('Actual File size of ' + filenames[1], ' : ', helloInfo.file_size)
print('Compressed File size of ' + filenames[1], ' : ', helloInfo.compress_size)
spamInfo = exampleZip.getinfo(filenames[0])
print('Actual File size of ' + filenames[0] + ': ', spamInfo.file_size)
print('Compressed File size of '+ filenames[0]+ ' : ' , spamInfo.compress_size)
print('Compressed file is %s times smaller!' % (round(spamInfo.file_size / spamInfo.compress_size, 4)))
Output
northcampus.zip namelist : ['North Campus (Faculty)Allotment.jpeg', 'hello.txt']
Information of hello.txt <ZipInfo filename='hello.txt' compress_type=deflate
external_attr=0x20 file_size=104 compress_size=86>
Actual File size of hello.txt : 104
Compressed File size of hello.txt : 86
Actual File size of North Campus (Faculty)Allotment.jpeg: 931045
Compressed File size of North Campus (Faculty)Allotment.jpeg : 913313
Compressed file is 1.0194 times smaller!
Extracting from ZIP Files
• The extract() method for ZipFile objects extracts a single file from the ZIP file.
• The extractall() method for ZipFile objects extracts all the files and folders from
a ZIP file into the current working directory.
• The string passed to extract() must match one of the strings in the list returned by
namelist().
• Optionally, a second argument can be passed to extract() function, to extract the
file into a folder other than the current working directory.
• If this second argument is a folder that doesn’t exist, Python will create the folder.
• The value that extract() returns is the absolute path to which the file was
extracted.
# Extract all files and folders
exampleZip.extractall('extra')
• It is seen that files inside the northcampus.zip had two files and were extracted into a
folder extra in D drive.
• Since extra folder was not existing, folder is created and files are stored
Creating and Adding to ZIP Files
• To create a compressed ZIP files, open the ZipFile object in write mode by
passing 'w' as the second argument.
• When a path argument is passed to the write() method of a ZipFile object,
Python compresses the file at that path and add it into the ZIP file.
• The first argument of write() method is a string of the filename to be added.
• The second argument is the compression type parameter, which tells the
computer what algorithm/type of compression to use to compress the files
• Just setting this value to zipfile.ZIP_DEFLATED, specifies the deflate
compression algorithm, which works well on all types of data.
import zipfile
newZip = zipfile.ZipFile('new.zip', 'w')
newZip.write(‘hello.txt', compress_type=zipfile.ZIP_DEFLATED)
newZip.close()
• This code creates a new ZIP file named new.zip that has the
compressed contents of hello.txt.
• NOTE - write mode erases all existing contents of a ZIP file.
• Hence, to add files to an existing ZIP file, open the ZIP file in
append mode, by passing 'a' as the second argument to
zipfile.ZipFile().
import zipfile
newZip = zipfile.ZipFile('new.zip', ‘a')
newZip.write(‘hello.txt', compress_type=zipfile.ZIP_DEFLATED)
newZip.close()
Assignment 8