Module 4- Part 1.
Files operations: Files and File Paths, The os. path Module, File Operations, Compressing Files
with the zip file, exception handling.
Duration: 3 Hrs
Prepared By [Link] Amardeep, Dept. Of ISE
Files and File Paths
A file has two key properties: a filename (usually written as one word) and a path. The path
specifies the location of a file on the computer.
For example,
C:\Users\asweigart\Documents\ [Link]
path → C:\Users\asweigart\Documents
Filename → [Link]
The part of the filename after the last period is called the file’s extension and tells you a file’s
type. [Link] is a Word document, and Users, asweigart, and Documents all refer to
folders(directories)
A directory is a collection of files and subdirectories. A directory inside a directory is known as
a subdirectory.
Figure 1- File in hierarchy of folders
Figure -1 shows this folder organization. The C:\ part of the path is the root folder, which contains all
other folders.
Backslash on Windows and Forward Slash on OS X and Linux
On Windows, paths are written using backslashes (\) as the separator between folder names. OS
X and Linux, however, use the forward slash (/) as their path separator.
To write Python scripts to handle both cases do with the [Link]() function. If you pass it
the string values of individual file and folder names in your path, [Link]() will return a
string with a file path using the correct path separators.
import os
[Link]('usr', 'bin', 'spam')
Output :
For Windows à 'usr\\bin\\spam'
OS X or Linux à 'usr/bin/spam'.
The [Link]() function is helpful if you need to create strings for filenames.
For example, the following example joins names from a list of filenames to the end of a folder’s
name:
myFiles = ['[Link]', '[Link]', '[Link]']
for filename in myFiles:
print([Link]('C:\\Users\\asweigart', filename))
Output:
C:\Users\asweigart\[Link]
C:\Users\asweigart\[Link]
C:\Users\asweigart\[Link]
The Current Working Directory
Every program that runs on your computer has a current working directory, or cwd. Any
filenames or paths that do not begin with the root folder are assumed to be under the current
working directory. You can get the current working directory as a string value with the
[Link]() function and change it with [Link]().
import os
[Link]()
Output:
'C:\\Python34'
[Link]('C:\\Windows\\System32')
[Link]()
Output:
'C:\\Windows\\System32'
Absolute vs. Relative Paths
There are two ways to specify a file path.
Ø An absolute path, which always begins with the root folder
Ø A relative path, which is relative to the program’s current working directory
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that
can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.”
Two periods (“dot-dot”) means “the parent folder.”
Figure 2- File paths
Absolute Path
The absolute path (also known as the full path) of an entity contains the complete information
(from the root to the ending) needed to locate it. The absolute path is not affected by the user’s
current working directory, and it always includes the root directory as a starting point.
Absolute paths are easier to understand, but they can be inconvenient to work with as each step
from the root to the entity needs to be included.
An absolute path always starts with a drive letter(such as C:, D:, etc) followed by colon and a
backslash.
Relative Path
The relative path is a path that is relative to the current working directory of the program or the
user. The relative path disregards the information needed to locate the current working directory
from the root directory and only focuses on the route from the working directory to the entity. It
describes the location of a file or folder without starting from the root of the file system.
Although relative paths hold less information than absolute paths, they are shorter and easier to
work with (especially in deeply nested directories).
The [Link] Module
The [Link] module contains many helpful functions related to filenames and file paths. For
instance, you’ve already used [Link]() to build paths in a way that will work on any
operating system. Since [Link] is a module inside the os module, you can import it by simply
running import os.
Handling Absolute and Relative Paths
The [Link] module provides functions for returning the absolute path of a relative path and for
checking whether a given path is an absolute path.
Ø Calling [Link](path) will return a string of the absolute path of the argument.
This is an easy way to convert a relative path into an absolute one.
Ø Calling [Link](path) will return True if the argument is an absolute path and False if
it is a relative path.
Ø Calling [Link](path, start) will return a string of a relative path from the start path
to path. If start is not provided, the current working directory is used as the start path.
Interactive Shell
>>> [Link]('.')
'C:\\Python34'
>>> [Link]('.\\Scripts')
'C:\\Python34\\Scripts'
>>> [Link]('.')
False
>>> [Link]([Link]('.'))
True
>>>[Link]('C:\\Windows', 'C:\\')
'Windows'
>>> [Link]('C:\\Windows', 'C:\\spam\\eggs')
'..\\..\\Windows'
>>> [Link]()
'C:\\Python34'
Ø Calling [Link](path) will return a string of everything that comes before the last
slash in the path argument.
Ø Calling [Link](path) will return a string of everything that comes after the last
slash in the path
Interactive Shell
>>> path = 'C:\\Windows\\System32\\[Link]'
>>> [Link](path)
'[Link]'
>>> [Link](path)
'C:\\Windows\\System32'
>>> calcFilePath = 'C:\\Windows\\System32\\[Link]'
>>> [Link](calcFilePath)
('C:\\Windows\\System32', '[Link]')
>>> ([Link](calcFilePath), [Link](calcFilePath))
('C:\\Windows\\System32', '[Link]')
>>> [Link]([Link])
['C:', 'Windows', 'System32', '[Link]']
Finding File Sizes and Folder Contents
The [Link] module provides functions for finding the size of a file in bytes and the files and
folders inside a given folder.
Ø Calling [Link](path) will return the size in bytes of the file in the path argument.
Ø Calling [Link](path) will return a list of filename strings for each file in the path
argument. (Note that this function is in the os module, not [Link].)
The functions in the interactive shell:
>>> [Link]('C:\\Windows\\System32\\[Link]')
776192
>>> [Link]('C:\\Windows\\System32')
['0409', '[Link]', '[Link]', '[Link]', '[Link]',
--snip--
'[Link]', '[Link]', 'zh-CN', 'zh-HK', 'zh-TW', '[Link]']
>>> totalSize = 0
>>> for filename in [Link]('C:\\Windows\\System32'):
totalSize = totalSize + [Link]([Link]('C:\\Windows\\System32', filename))
>>> print(totalSize)
1117846456
Checking Path Validity
The [Link] module provides functions to check whether a given path exists and whether it is a
file or folder.
Ø Calling [Link](path) will return True if the file or folder referred to in the argument
exists and will return False if it does not exist.
Ø Calling [Link](path) will return True if the path argument exists and is a file and
will return False otherwise.
Ø Calling [Link](path) will return True if the path argument exists and is a folder and
will return False otherwise.
The functions in the interactive shell:
>>> [Link]('C:\\Windows')
True
>>> [Link]('C:\\some_made_up_folder')
False
>>> [Link]('C:\\Windows\\System32')
True
>>> [Link]('C:\\Windows\\System32')
False
>>> [Link]('C:\\Windows\\System32\\[Link]')
False
>>> [Link]('C:\\Windows\\System32\\[Link]')
True
>>> [Link]('D:\\')
True
File Operations
The File Reading/Writing Process
Plaintext files contain only basic text characters and do not include font, size, or color
information. Text files with the .txt extension or Python script files with the .py extension are
examples of plaintext files. These can be opened with Windows’s Notepad or OS X’s TextEdit
application. Your programs can easily read the contents of plaintext files and treat them as an
ordinary string value.
Binary files are all other file types, such as word processing documents, PDFs, images,
spreadsheets, and executable programs.
There are three steps to reading or writing files in Python.
1. Call the open() function to return a File object.
2. Call the read() or write() method on the File object.
3. Close the file by calling the close() method on the File object.
Opening Files with the open() Function
To open a file with the open() function, you pass it a string path indicating the file you want to
open; it can be either an absolute or relative path. The open() function returns a File object.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
The following into the interactive shell:
>>> helloFile = open('C:\\Users\\your_home_folder\\[Link]')
Reading the Contents of Files
Now that you have a File object, you can start reading from it. If you want to read the entire
contents of a file as a string value, use the File object’s read() method.
>>> helloContent = [Link]()
If you think of the contents of a file as a single large string value, the read() method returns the
string that is stored in the file. Alternatively, you can use the readlines() method to get a list of
string values from the file, one string for each line of text.
>>> sonnetFile = open('[Link]')
>>> [Link]()
Writing to Files
To write to an existing file, you must add a parameter to the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
If the filename passed to open() does not exist, both write and append mode will create a new,
blank file. After reading or writing a file, call the close() method before opening the file again.
>>> baconFile = open('[Link]', 'w')
>>> [Link]('Hello world!\n')
>>> [Link]()
>>> baconFile = open('[Link]', 'a')
>>> [Link]('Bacon is not a vegetable.')
>>> [Link]()
>>> baconFile = open('[Link]')
>>> content = [Link]()
>>> [Link]()
>>> print(content)
Hello world!
Bacon is not a vegetable.
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 destination 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
>>> import shutil, os
>>> [Link]('C:\\')
u >>> [Link]('C:\\[Link]', 'C:\\delicious')
'C:\\delicious\\[Link]'
v >>> [Link]('[Link]', 'C:\\delicious\\[Link]')
'C:\\delicious\\[Link]'
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
u, the original [Link] filename is used for the new, copied file’s filename.
The second [Link]() call v also copies the file at C:\[Link] to the folder C:\delicious but
gives the copied file the name [Link].
While [Link]() will copy a single file, [Link]() will copy an entire folder and every
folder and file contained in it. 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.
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, enter the following into the interactive shell:
>>> import shutil
>>> [Link]('C:\\[Link]', 'C:\\eggs')
'C:\\eggs\\[Link]'
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().
The destination path can also specify a filename. In the following example, the source file is
moved and renamed.
>>> [Link]('C:\\[Link]', 'C:\\eggs\\new_bacon.txt')
'C:\\eggs\\new_bacon.txt'
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.”
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.
Ø Calling [Link](path) will delete the file at path.
Ø Calling [Link](path) will delete the folder at path. This folder must be empty of any
files or folders.
Ø Calling [Link](path) will remove the folder at path, and all files and folders it
contains will also be deleted.
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
Figure 3- Zip File
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 you saw returned by the
open() function .They are values through which the program interacts with the file.
To create a ZipFile object, call the [Link]() function, passing it a string of the .zip file’s
filename. Note that zipfile is the name of the Python module, and ZipFile() is the name of the
function.
For example, enter the following into the interactive shell:
>>> import zipfile, os
>>> [Link]('C:\\') # move to the folder with [Link]
>>> exampleZip = [Link]('[Link]')
>>> [Link]()
['[Link]', 'cats/', 'cats/[Link]', 'cats/[Link]']
>>> spamInfo = [Link]('[Link]')
>>> spamInfo.file_size
13908
>>> spamInfo.compress_size
3828
u >>> 'Compressed file is %sx smaller!' % (round(spamInfo.file_size / spamInfo
.compress_size, 2))
'Compressed file is 3.63x smaller!'
>>> [Link]()
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.
The extract() method for ZipFile objects will extract a single file from the ZIP file.
Creating and Adding to ZIP Files
ZIP is an archive file format that supports lossless data compression.
ZIP file offer an ideal way to make large files smaller and keep related files together.
Need for ZIP file:
• To reduce storage requirements.
• To improve transfer speed over standard connections.
To create your own compressed ZIP files, you must open the ZipFile object in write mode by
passing 'w' as the second argument. (This is similar to opening a text file in write mode by
passing 'w' to the open() function.)
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. (This specifies the deflate compression algorithm, which works well
on all types of data.)
Enter the following into the interactive shell:
>>> import zipfile
>>> newZip = [Link]('[Link]', 'w')
>>> [Link]('[Link]', compress_type=zipfile.ZIP_DEFLATED)
>>> [Link]()
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.
Exceptions Handling
Python raises an exception whenever it tries to execute invalid code. In Python, exceptions
with try and except statements, so that your program can recover from exceptions that you
anticipated.
Exceptions are raised with a raise statement. In code, a raise statement consists of the following:
Ø The raise keyword
Ø A call to the Exception() function
Ø A string with a helpful error message passed to the Exception() function
For example, enter the following into the interactive shell:
>>> raise Exception('This is the error message.')
Traceback (most recent call last):
File "<pyshell#191>", line 1, in <module>
raise Exception('This is the error message.')
Exception: This is the error message.
If there are no try and except statements covering the raise statement that raised the exception,
the program simply crashes and displays the exception’s error message.
Example : Raise an error and stop the program if x is lower than 0:
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")
***********
Question Bank:
1. Describe the difference between Python os and [Link] modules. Also, discuss the following
methods of os module
(i) chdir()
(ii) rmdir()
(iii) listdir()
(iv) getcwd()
(v) makedirs()
2. Discuss the following methods of [Link] module.
(i) isfile()
(ii) abspath()
(iii) relpath()
(iv) dirname()
(v) isabs()
(vi) basename()
(vii) split()
(viii) listdir()
(ix) getsize()
(x) isdir()
(xi) exists()
3. Consider the file structure given below. Write Python program to delete all the files and
subdirectories from the Extinct_Animals Directory.
4. Explain with suitable Python program segments:
(i) [Link]()
(ii) [Link]()
5. Develop a Python program find the total size of all the files in the given directory.
6. Explain permanent delete and safe delete with a suitable Python programming example to
each.
7. 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.
8. Explain in briefly, What are the different methods of file operations supports in python
shutil module
9. Write a note on Raising exceptions in files in Python
10. Show that files and folders can be copied using Shutil module
11. Differentiate between Absolute and relative paths in specifying file paths
12. Describe the different access modes of the files with an example.