INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
MODULE-3 FILES
1. 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
Ex: There’s a file with the filename functions.docx in the path E:\LK-CLASS\Module3\functions.docx The
part of the filename after the last period is called the file’s extension and tells you a file’s type. functionst.docx
is a Word document, and LK-CLASS and Module3 refer to folders (also called directories). Folders can
contain files and other folders. For example, functions.docx
is in the Module3 folder, which is inside the LK-CLASS folder. The E:\ part of the path is the root folder,
which contains all other folders
1.1 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.
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')
'usr\\bin\\spam'
os.path.join('usr', 'bin', 'spam') returned 'usr\\bin\\spam'
(Notice that the backslashes are doubled because each backslash needs to be escaped by another backslash
character.)
The os.path.join() function is helpful if you need to create strings for filenames. These strings will be passed
to several of the file-related functions Ex: The following example joins names from a list of filenames to the
end of a folder’s name:
>>> myFiles = ['accounts.txt', 'tax.csv', 'invite.docx']
>>> for filename in myFiles:
print(os.path.join('E:\\LK-CLASS\\Module3', filename))
output:
'E:\LK-CLASS\Module3\accounts.txt
'E:\LK-CLASS\Module3\tax.csv
'E:\LK-CLASS\Module3\invite.docx
1.2 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()
'C:\\Python34'
>>> os.chdir('E:\\LK-CLASS\\Module3')
>>> os.getcwd()
'E:\\LK-CLASS\Module3'
Here, the current working directory is set to C:\Python34, so the filename functions.docx refers to C:\Python34\functions.docx.
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 1 of 7
INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
When we change the current working directory, it is interpreted as E:\\LK-CLASS\Module3/functions.docx
Python will display an error if you try to change to a directory that does not exist.
1.3 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.”
When the current working directory is set to C:\bacon, the relative paths for the other folders and files are set as follows:
Relative Path Absolute Path
C:\ ..\ C:\
.\ C:\bacon
bacon .\fizz C:\bacon/fizz
fizz
.\fizz\spam.txt C:\bacon\fizz\spam.txt
spam.txt .
spam.txt .\spam.txt C:\bacon\spam.txt
eggs ..\eggs C:\eggs
spam.txt ..\eggs\spam.txt C:\eggs\spam.txt
spam.txt ..\spam.txt C:\spam.txt
1.4 Creating New Folders with os.makedirs()
Your programs can create new folders (directories) with the os.makedirs() function.
>>> import os
>>> os.makedirs(‘E:\LK’)
Note: LK directory (folder) will be created in E:\
>>> os.makedirs('E:\LK\Class\2023')
It first creates E:\LK folder, inside that Class folder and inside Class folder another folder named 2023.
It’s a hierarchical structure.
os.makedirs() will create any necessary intermediate folders in order to ensure that the full path exists.
1.5 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. Whenever your programs need
to work with files, folders, or file paths, import the os module.
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 2 of 7
INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
1.6 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.
>>> 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 argument.
C:\Windows\System32\calc.exe
Dir name Base name
Note: The base name follows the last slash in a path and is the same as the filename. The dir name is everything
before the last slash.
Ex:
import os
fpath='E:\LK\Progs\COs.pdf'
os.path.basename(fpath)
output:
'COs.pdf'
import os
fpath='E:\LK\Progs\COs.pdf'
os.path.dirname(fpath)
output:
'E:\\8051\\Progs'
If you need a path’s directory name and base name together, you can just call os.path.split() to get a tuple
value with these two strings.
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 3 of 7
INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
import os
fpath='E:\8051\Progs\COs.pdf'
os.path.split(fpath)
output:
('E:\\8051\\Progs', 'COs.pdf')
Note: os.path.split() does not take a file path and return a list of strings of each folder. For that, use the split()
string method and split on the string in os.sep.
fpath.split(os.path.sep)
['E:', '8051', 'Progs', 'COs.pdf']
1.7 Finding File Sizes and Folder Contents
Once you have ways of handling file paths, you can then start gathering information about specific files and
folders. 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.)
os.path.getsize('E:\8051\Progs\COs.pdf')
167480
os.listdir('E:\8051\Progs')
['7SEG.C',
'COs.pdf',
'dac for adc.c',
'Dcmotor.c',
'delay.c',
'hexkpd.c',
'keyboard display.c',
'KeyBrd.c',
'Lcd.h',
'McbElev.c',
'patch.asm',
'PROG-21.ASM',
'prog1.asm',
'prog10.asm']
1.8 Checking Path Validity
Many Python functions will crash with an error if you supply them with a path that does not exist. 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.
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 4 of 7
INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
Calling os.path.isdir(path) will return True if the path argument exists and is a folder and will return False
otherwise
import os
>>> os.path.exists('C:\\Windows')
True
>>> os.path.exists('C:\\Hello’)
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
2. Reading the Content of Files
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.
Ex:
f1=open("E:\Sample.txt",'r')
content=f1.read()
print (content)
output:
Goodmorning all. I'm Prof. Krishnananda L, Faculty at GECM.
Welcome to Python class. This class is for sem2 EC students.
Note1: The call to open() returns a File object. A File object represents a file on your computer. We stored the
File object in the variable f1. Now, whenever you want to read from or write to the file, you can do so by
calling methods on the File object in f1.
Note2: When a file is opened in read mode, Python lets you only read data from the file; you can’t write or
modify it in any way
3. Writing to Files
Python allows you to write content to a file. you need to open it in write mode and append mode Write mode
will overwrite the existing file and start from scratch Append mode, on the other hand, will append text to the
end of the existing file. Note that the write() method does not automatically add a newline character to the end
of the string like the print() function does
4. Saving Variables with the shelve Module
You can save variables in your Python programs to binary shelf files using the shelve module. This way, your
program can restore data to variables from the hard drive.
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 5 of 7
INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
To read and write data using the shelve module, you first import shelve. Call shelve.open() and pass it a
filename, and then store the returned shelf value in a variable. You can make changes to the shelf value as if
it were a dictionary.
import shelve
s = shelve.open('D:\mydata')
fruits = ['apple', 'mango', 'orange']
s['fruits'] = fruits
s.close()
Explanation:
Here, our shelf value is stored in s. We create a list cats and write s['fruits'] = cats to store the list in s as a
value associated with the key 'fruits' (like in a dictionary). Then we call close() on shelf value s.
Note: After running the previous code on Windows, you will see3 new files in the directory: mydata.bak,
mydata.dat, and mydata.dir. These binary files contain the data you stored in your shelf.
The module frees you from worrying about how to store your program’s data to a file.
Programs can use the shelve module to later reopen and retrieve the data from these shelf files. Shelf
values don’t have to be opened in read or write mode—they can do both once opened
>>> s1 = shelve.open('mydata')
>>> type(s1)
<class 'shelve.DbfilenameShelf'>
>>> s1['fruits']
['apple', 'mango', 'orange']
>>> s1.close()
Note: Entering s1['fruits'] returns the same list that we stored earlier
Just like dictionaries, shelf values have keys() and values() methods that will return list-like values of
the keys and values in the shelf. You should pass them to the list() function to get them in list form
>>> s1 = shelve.open('mydata')
>>> list(s1.keys())
['fruits']
>>> list(s1.values())
[['apple', 'mango', 'orange']]
>>> s1.close()
Note: Plaintext is useful for creating files that you’ll read in a text editor such as Notepad or TextEdit, but if
you want to save data from your Python programs, use the shelve module.
4. Saving Variables with the pprint.pformat() Function
The pprint.pprint() function will “pretty print” the contents of a list or dictionary to the screen, while
the pprint.pformat() function will return this same text as a string instead of printing it.
Say you have a dictionary stored in a variable and you want to save this variable and its contents for
future use. Using pprint.pformat() will give you a string that you can write to .py file. This file will be
your own module that you can import whenever you want to use the variable stored in it.
saving data using the shelve module is the preferred way to save variables to a file.
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 6 of 7
INTRODUCTION TO PYTHON PROGRAMMING BPLCK205B MODULE-2
import pprint
fruits = [{'name': 'apple', 'color': 'red'}, {'name': 'mango', 'color': 'yellow'}]
pprint.pprint(fruits)
pprint.pformat(fruits)
output:
[{'color': 'red', 'name': 'apple'}, {'color': 'yellow', 'name': 'mango'}]
"[{'color': 'red', 'name': 'apple'}, {'color': 'yellow', 'name': 'mango'}]"
fObj = open('D:/Newfruits.py', 'w')
fObj.write('fruits = ' + pprint.pformat(fruits) + '\n')
fObj.close()
Explanation:
We have a list of dictionaries, stored in a variable fruits. To keep the list in fruits available even after we
close the shell, we use pprint.pformat() to return it as a string. Once we have the data in fruits as a string,
it’s easy to write the string to a file, which we’ll call Newfruits.py.
Note: The modules that an import statement imports are themselves just Python scripts. The file is a module
that can be imported just like any other module. And since Python scripts are themselves just text files with
the .py file extension, Python programs can even generate other Python programs.
import Newfruits
Newfruits.fruits
Output:
[{'color': 'red', 'name': 'apple'}, {'color': 'yellow', 'name': 'mango'}]
Newfruits.fruits[0]
{'color': 'red', 'name': 'apple'}
Newfruits.fruits[1]['name']
‘mango’
Compiled by: Prof. Krishnananda L, Dept of ECE, GEC Mosalehosahalli, Hassan Page 7 of 7