Module-04 Organizing Files
Module-04 Organizing Files
1
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.
2
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.
• If destination is a filename, that filename will be used as the new name
of the copied file.
3
• The first shutil.copy() call copies the file at
C:\Users\Al\spam.txt to the folder C:\Users\Al\some_folder.
4
• shutil.copy() will copy a single file.
• shutil.copytree() will copy an entire folder and every folder and file
contained in it.
• 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.
• os.rmdir(path) will delete the folder at path. This folder must be empty of
any files or folders.
• shutil.rmtree(path) will remove the folder at path, and all files and folders
it contains will also be deleted.
>>>import os
for filename in os.listdir():
if filename.endswith('.rxt'):
os.unlink(filename)
7
Safe Deletes with the send2trash Module
8
Walking a directory tree
Traversing through a directory (folder) and all its subfolders to
perform some operation on files or folders within them
import os
for folderName, subfolders, filenames in os.walk('C:\\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('')
APPLICATION DEVELOPMENT
USING PYTHON – 18CS55 9
10
Cont...
• You can use os.walk() in a for loop statement to walk a
directory tree,
• 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
11
Compressing files with the zipfile module
• ZIP files (with the .zip file extension), can hold the compressed
contents of many other files.
• Compressing a file reduces its size, which is useful when
transferring it over the Internet.
• 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 attached to an email.
• Python programs can both create and open (or extract) ZIP files
using functions in the zipfile module.
12
Reading ZIP Files
• To read the contents of a ZIP file, first you must create a ZipFile
object.
13
14
• 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.
• 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.
15
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.
>>> exampleZip.extract('spam.txt')
'C:\\spam.txt'
>>> exampleZip.extract('spam.txt', 'C:\\some\\new\\folders')
'C:\\some\\new\\folders\\spam.txt'
>>> exampleZip.close()
17
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 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
• zipfile.ZIP_DEFLATED.
18
>>> import zipfile
>>> newZip = zipfile.ZipFile('new.zip', 'w')
>>> newZip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED)
>>> newZip.close()
19