Module – 3
Chapter: 8 Reading and Writing Files
File:
• A file is a collection of data stored on a computer’s hard
drive or other storage device.
• Files are used to store and organize data to make it easily
accessible and retrievable.
• A file typically has the following characteristics/properties:
i)filename: A unique identifier given to the file, often with
an extension(e.g., .txt, .jpg, .docx, .mp3, .mp4, .exe)
indicating its type
ii) path: specifies the location of a file on the computer.
Eg., c:\Users\asweigart\Document\[Link]
C:\ part of the path is the root folder, which contains all other
folders
On windows the root folder is C:\ or C: drive and the paths are
written using backslashes (\) as the path separator
On OS X and Linux the root folder is / and the paths are
written using forward slash (/)
iii)Size: the amount of storage space occupied by the file in
bytes.
iv) type: the files format or category (e.g., document, image,
video, executable)
v) Attributes: Additional metadata, such as permissions,
ownership, and timestamps, that describe the file’s properties
and behavior.
How to make your python script work on all
operating system?
• For building the paths in a way that is compatible with
different operating system we have [Link]()
function in python
• [Link]() function in the os module is used to join
one or more path components together to form a
complete path.
• It is a safe way to concatenate paths, as it takes into
account the operating systems path separator(i.e.,
backward slash(\) for windows and forward slash(/) for OS
X and Linux )
• The function takes multiple arguments as string values of
individual folders and file name and returns a single string
representing the joined path
• Eg.,
O/P
[Link]() function is helpful if you need to create strings for file name
Program to join names from a list of filenames to the end of a
folder’s name
O/P:
CURRENT WORKING DIRECTORY (cwd)
• It is the directory in which the user is currently
working
• cwd is the default location where the files are saved
and where the command prompt or terminal looks
for files when a command is entered without
specifying a full path
• In python, you can get the current working directory
using the function [Link]()
O/P:
You can also change the current working directory using
the [Link]() function
import os
[Link](“/path/to/new/directory”)
This will change the current working directory to the
specified path
Python will display an error if you try to change to a
directory that does not exist
Absolute Vs Relative path
There are 2 ways to specify a file path
Absolute Path Relative Path
Is a complete path that Is a partial path that specifies
specifies the location of a file the location of a file or
or directory from the root directory relative to the
directory of the file system current working directory
It includes all the directories It does not include the root
and subdirectories needed to directory or all the directories
locate the file or directory and subdirectories needed to
locate the file or directory
Always begins from root Always begins with -
directory Dot (.) which means ‘this
directory’
Dot-dot (..) which means ‘the
parent folder’
Eg.,c:\bacon\fizz\[Link] Eg., .\fizz\[Link]
The .\ at the start of a relative path is optional. For example,
.\[Link] and [Link] refer to the same file.
Creating new folders with [Link]() function
New folders/directories can be created using [Link]()
function in os module
o/p:
This will create not just the C:\delicious folder but also a
walnut folder inside C:\delicious and a waffles folder inside
C:\delicious\walnut.
That is, [Link]() will create any necessary intermediate
folders in order to ensure that the full path exists. Figure 8-3
shows this hierarchy of folders.
The [Link] Module
• [Link] module is a module inside os module, can be
imported by simply running ,
import os
• The [Link] module contains many helpful functions
which provides a way to manipulate and manage file paths
and directories
• Provides functions such as(Handling Absolute & Relative Path):
i) [Link](path)
• this function will return a string of the absolute path
of the argument.
• An easy way to convert a relative path into an
absolute one
• Eg.,1
Since C:\Python34 was the working directory when
[Link]() was called, the “single-dot” folder
represents the absolute path 'C:\\Python34'.
Eg., 2.
ii) [Link](path)
• Calling this function will return True if the argument is an
absolute path and False if it is a relative path
• eg.,1.
• eg.2.
iii) [Link](path, start)
• Calling this function will return a string of a relative path
from the start path to path
• If start path is not provided, the current working directory
is used as the start path.
• Eg., 1.
• Eg., 2.
iv) [Link](path)
• Calling this function will return a string of everything that
comes before the last slash in the path argument.
• Eg.,
v) [Link](path)
• Calling this function will return a string of everything that
comes after the last slash in the path argument.
• Eg.,
vi) [Link]()
• This function gives you the path’s dirname and base
name together as a tuple value.
• Eg.,
Note: you can create the same tuple by calling [Link]() and
[Link]() and placing their return values in a tuple.
But [Link]() is a nice shortcut if you need both values.
• [Link]() do not return a list of strings of each folder.
To get the list of strings of each folder use split() string
method and split on the string in [Link]
Eg.,
**split([Link]) is used to split a path into a list of directories or
file names. It takes the path separator ([Link]) as the separator to
split the path.
• On OS X and Linux systems, there will be a blank string at
the start of the returned list:
• Eg.,
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.
i)[Link](path)
• This function when called will return the file size in bytes
located at the specified path.
• Eg.,
ii) [Link](path)
• calling this function 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].
• Eg.,
[‘[Link]’, ‘[Link]’, ‘[Link]’]
Program to find the total size of all the files in a
directory
___________________________________________
o/p: 45777
• In the above program, we loop over each file name in the
C:\Windows\System32 folder.
• Using [Link]() each individual file is got which is the
input for [Link]()
• The integer that [Link]() returns is added to the
value of totalSize and printed.
Checking path validity
• Programs crash with an error if supplied with the path
that doesn’t exist
• The [Link] module provides functions to check whether
a given path exists and whether it is a file or folder.
i) [Link](path)
• Calling this function will return True if the file or folder
referred to in the argument exists and will return False if
it does not exist.
• Eg., 1
• Eg., 2
ii) [Link](path)
• Calling this function will return True if the path argument
exists and is a file and will return False otherwise.
• Eg., 1
Eg., 2
iii) [Link](path)
• Calling this function will return True if the path argument
exists and is a folder and will return False otherwise.
• Eg., 1
Eg., 2
Note: we can determine whether there is a DVD or flash drive
currently attached to the computer by checking for it with the
[Link]() function. Additional volumes such as DVD drive or
USB will appear on the windows as new lettered root drive such as
D:\ or E:\
False indicates that additional volume is not plugged to
computer.
The File Reading /Writing Process
• A file is a collection of data stored on a computer’s hard
drive or other storage device.
• ASCII file and Binary file are two types of file formats used
to store data on a computer.
i) ASCII file:
- Stores data as plain text, using only characters from the
ASCII character set (letters, numbers, punctuation, and
control characters).
- Each character is represented by a single byte (8 bits).
- Human-readable and can be edited with a text editor.
- Examples: text files (.txt), configuration files (.ini), and
batch files (.bat).
ii) Binary file:
- Stores data in a binary format, using all possible values
of each byte (0-255).
- Can store any type of data, including images, audio,
video, and executable code.
- Not human-readable, and requires a specific program
or interpreter to read and understand the data. If you
open a binary file in Notepad or TextEdit, it will look like
scrambled nonsense, shown in below figure
- Examples: image files (.jpg, .png), executable files
(.exe), and compressed files (.zip).
There are three steps to reading or writing plaintext/ASCII
files in Python.
i. Call the open() function to return a File object.
ii. Call the read() or write() method on the File object.
iii. Close the file by calling the close() method on the File
object
**File Object provides a way to interact with the file such as reading, writing
and manipulating its contents. This File Object contains information about the file
such as its name and mode.
i) Opening Files with the open() function
• A file is opened using open() function, which takes
two arguments:
i) path: The path to the file you want to open. The
path argument can be absolute or relative.
ii) mode: the mode in which you want to open the
file(e.g., ‘r’ for read, ‘w’ for write, ‘a’ for append)
• The open() function returns a file object- which
provide a way to interact with the file like reading and
writing to file
• Syntax:
FileObj=open(path, mode)
Optional parameter to open
function, If not mentioned, then by
Calling open ()function
Absolute or relative default the file is opened in read
will return File Object
path to the file you mode
which is stored in the
variable FileObj want to open
• Eg., create a text file named [Link] in the path
C:\users\admin\[Link]
Now to open this [Link] text file use the function
open()
helloFile=open(‘c:\\users\\admin\\[Link]’)
This command will open the file in ‘reading plaintext’
mode or read mode for short which is the default
mode for the files you open in python
When a file is opened in read mode, Python lets you
only read data from the file; cannot write to it or
modify it in any way.
To write to a file, the file must be explicitly opened in
write mode(‘w’) or append mode(‘a’)
‘w’ mode- will overwrite the existing file and
start from scratch.
‘a’mode- will append text to the end of the
existing file.
The open function returns a File Object which you
stored in the variable helloFile. Read and Write
operations on the file can be done by calling methods
on the file object in helloFile.
ii) Reading the contents of the file using read()
method
Once we have the file object from the open()
function, we can read the entire contents of the file or
read line by line using read() and readlines() method.
The read method returns the entire contents of a file
as a single string value
Whereas the readlines() method returns a list of
string values from the file, one string for each line of
text.
When a file is opened in read mode, Python lets you
only read data from the file; cannot write to it or
modify it in any way.
[Link]
• Eg., create a text file named [Link] Hi! I am ABC.
nd
in the path C:\users\admin\[Link] I am in 2 sem CSE.
Python is my favorite subject.
Next open [Link] in read mode.
------------------------------------------------------------------
FileObj=open(‘c:\\users\\admin\\[Link]’, ‘r’)
SampleContents1=[Link]()
SampleContents2=[Link]()
-------------------------------------------------------------------
The variable SampleContents1 holds a single string
value ie.,
SampleContents1= ‘Hi! I am ABC.\n I am in 2nd sem CSE.\n
Python is my favorite subject.’
The variable SampleContents2 holds a list of string values
from the file i.e,
SampleContents2=[‘ Hi! I am ABC.\n’, ‘I am in 2nd sem CSE.\n’,
‘Python is my favorite subject.\n’ ]
Note: A list of string is often easier to work with than a single large string value.
iii) Writing to files
Python allows you to write content to a file using the
open() function in:
i) write mode(‘w’) or
ii) append mode(‘a’)
i) write mode(‘w’)
• Pass ‘w’ as the second argument to open() function to
open the file in write mode
• Write mode will overwrite the existing file and start
from scratch
• Eg.,
SpamFile=open(‘[Link]’, ‘w’)
[Link](‘Hello World!\n’)
[Link]()
This will create a new file called ‘[Link]’ and write the string
‘Hello World!\n’ to it. If this file already exists, the contents
of the file will be overwritten.
Finally we need to call close() method before opening the file
again.
ii) append mode(‘a’)
• Pass ‘a’ as the second argument to open() function to
open the file in append mode.
• Append mode will append text to the end of the existing
file.
• Eg.,
SpamFile=open(‘[Link]’, ‘a’)
[Link](‘Welcome to Python Programming!\n’)
[Link]()
The above piece of code will append the string ‘Welcome to
Python Programming!\n’ at the end of the file [Link].
[Link]
Hello world!
Welcome to Python Programming!
Note: if the filename passed to open() doesn’t exist, both write and append
mode will create a new file.
After reading or writing a file, call the close() method before opening the file again.