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

Module 4

Uploaded by

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

Module 4

Uploaded by

sushma-icb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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 Dr.Rashmi 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\ projects.docx

path → C:\Users\asweigart\Documents

Filename → projects.docx

The part of the filename after the last period is called the file’s extension and tells you a file’s
type. project.docx 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 os.path.join() function. If you pass it
the string values of individual file and folder names in your path, os.path.join() will return a
string with a file path using the correct path separators.

import os

os.path.join('usr', 'bin', 'spam')

Output :

For Windows à 'usr\\bin\\spam'

OS X or Linux à 'usr/bin/spam'.

The os.path.join() 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 = ['accounts.txt', 'details.csv', 'invite.docx']

for filename in myFiles:

print(os.path.join('C:\\Users\\asweigart', filename))

Output:

C:\Users\asweigart\accounts.txt

C:\Users\asweigart\details.csv

C:\Users\asweigart\invite.docx

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
os.getcwd() function and change it with os.chdir().

import os

os.getcwd()

Output:

'C:\\Python34'

os.chdir('C:\\Windows\\System32')

os.getcwd()

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 os.path Module

The os.path module contains many helpful functions related to filenames and file paths. For
instance, you’ve already used os.path.join() to build paths in a way that will work on any
operating system. Since os.path is a module inside the os module, you can import it by simply
running import os.

Handling Absolute and Relative Paths

The os.path module provides functions for returning the absolute path of a relative path and for
checking whether a given path is an absolute path.

Ø Calling os.path.abspath(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 os.path.isabs(path) will return True if the argument is an absolute path and False if
it is a relative path.

Ø Calling os.path.relpath(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

>>> os.path.abspath('.')

'C:\\Python34'

>>> os.path.abspath('.\\Scripts')

'C:\\Python34\\Scripts'

>>> os.path.isabs('.')

False

>>> os.path.isabs(os.path.abspath('.'))

True

>>>os.path.relpath('C:\\Windows', 'C:\\')

'Windows'

>>> os.path.relpath('C:\\Windows', 'C:\\spam\\eggs')

'..\\..\\Windows'

>>> os.getcwd()

'C:\\Python34'
Ø Calling os.path.dirname(path) will return a string of everything that comes before the last
slash in the path argument.

Ø Calling os.path.basename(path) will return a string of everything that comes after the last
slash in the path

Interactive Shell

>>> path = 'C:\\Windows\\System32\\calc.exe'

>>> os.path.basename(path)

'calc.exe'

>>> os.path.dirname(path)

'C:\\Windows\\System32'

>>> calcFilePath = 'C:\\Windows\\System32\\calc.exe'

>>> os.path.split(calcFilePath)

('C:\\Windows\\System32', 'calc.exe')

>>> (os.path.dirname(calcFilePath), os.path.basename(calcFilePath))

('C:\\Windows\\System32', 'calc.exe')

>>> calcFilePath.split(os.path.sep)

['C:', 'Windows', 'System32', 'calc.exe']

Finding File Sizes and Folder Contents

The os.path module provides functions for finding the size of a file in bytes and the files and
folders inside a given folder.

Ø Calling os.path.getsize(path) will return the size in bytes of the file in the path argument.
Ø Calling os.listdir(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 os.path.)

The functions in the interactive shell:

>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')

776192

>>> os.listdir('C:\\Windows\\System32')

['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',

--snip--

'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']

>>> totalSize = 0

>>> for filename in os.listdir('C:\\Windows\\System32'):

totalSize = totalSize + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))

>>> print(totalSize)

1117846456

Checking Path Validity

The os.path module provides functions to check whether a given path exists and whether it is a
file or folder.

Ø Calling os.path.exists(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 os.path.isfile(path) will return True if the path argument exists and is a file and
will return False otherwise.

Ø Calling os.path.isdir(path) will return True if the path argument exists and is a folder and
will return False otherwise.

The functions in the interactive shell:

>>> os.path.exists('C:\\Windows')

True

>>> os.path.exists('C:\\some_made_up_folder')
False

>>> os.path.isdir('C:\\Windows\\System32')

True

>>> os.path.isfile('C:\\Windows\\System32')

False

>>> os.path.isdir('C:\\Windows\\System32\\calc.exe')

False

>>> os.path.isfile('C:\\Windows\\System32\\calc.exe')

True

>>> os.path.exists('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\\hello.txt')

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 = helloFile.read()

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('sonnet29.txt')

>>> sonnetFile.readlines()
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('bacon.txt', 'w')

>>> baconFile.write('Hello world!\n')

>>> baconFile.close()

>>> baconFile = open('bacon.txt', 'a')

>>> baconFile.write('Bacon is not a vegetable.')

>>> baconFile.close()

>>> baconFile = open('bacon.txt')

>>> content = baconFile.read()

>>> baconFile.close()

>>> 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 shutil.copy(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

>>> os.chdir('C:\\')

u >>> shutil.copy('C:\\spam.txt', 'C:\\delicious')

'C:\\delicious\\spam.txt'

v >>> shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt')

'C:\\delicious\\eggs2.txt'

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
u, the original spam.txt filename is used for the new, copied file’s filename.

The second shutil.copy() call v also copies the file at C:\eggs.txt to the folder C:\delicious but
gives the copied file the name 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. 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.

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, enter the following into the interactive shell:

>>> import shutil

>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')

'C:\\eggs\\bacon.txt'
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().

The destination path can also specify a filename. In the following example, the source file is
moved and renamed.

>>> shutil.move('C:\\bacon.txt', 'C:\\eggs\\new_bacon.txt')

'C:\\eggs\\new_bacon.txt'

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.”

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 os.unlink(path) will delete the file at path.

Ø Calling os.rmdir(path) will delete the folder at path. This folder must be empty of any
files or folders.

Ø Calling shutil.rmtree(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 example.zip 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 zipfile.ZipFile() 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

>>> os.chdir('C:\\') # move to the folder with example.zip

>>> exampleZip = zipfile.ZipFile('example.zip')

>>> exampleZip.namelist()

['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']

>>> spamInfo = exampleZip.getinfo('spam.txt')

>>> 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!'

>>> exampleZip.close()

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 = zipfile.ZipFile('new.zip', 'w')

>>> newZip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED)

>>> newZip.close()

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.

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 os.path 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 os.path 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) os.path.basename()

(ii) os.path.join()

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.

You might also like