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

File Handling in Python PDF

Uploaded by

Daksh Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

File Handling in Python PDF

Uploaded by

Daksh Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

File Handling in Python

What is a fi le ?

• File is a named location on disk to store related


information. It is used to permanently store data in a
non-volatile memory (e.g. hard disk).
• Since, random access memory (RAM) is volatile which loses
its data when computer is turned off , we use files for
future use of the data.
• When we want to read from or write to a file we need
to open it first. When we are done, it needs to be
closed, so that resources that are tied with the file are
freed.
• Hence, in Python, a file operation takes place in the
following order.
– Open a file
– Read or writ e (perf orm operat ion)
– Close the file
Opening a fi le

• Python has a built-in function open() to open a


file. This function returns a fi le object, also
called a handle, as it is used to read or modify
t he file accordingly.
>>> f = open("test.txt")
• We can specify the mode while opening a file.
• In mode, we specify whether we want to read
'r', write 'w' or append 'a' to the file.
• We also specify if we want to open the fi le
in text mode or binary mode.
• The default is reading in text mode.
Python file modes
Examples

• f = open("test.txt")
# equivalent to 'r' or 'rt'
• f = open("test.txt",'w')
# write in text mode
•f =
open("img.bmp",'r+b')
# read and write in
binary mode
• f = open("test.txt",mode =
'r',encoding = 'utf-8')
# Specfied with encoding
Reading functions

• read(n)
– Read at most n charact ers form t he fi le. Reads
t ill end of file if it is negative or None.
• readable()
– Returns True if the file stream can be read
from.
• readline(n=-1)
– Read and ret urn one line from t he fi le.
Reads in at most n bytes if specified.
• readlines(n=-1)
– Read and return a list of lines from the file.
Reads in at most n bytes/characters if
specified.
read( )
readline( )
readlines( )
Example:

• Read a given file and count total number


of ‘the’ in the given file.
Solution
Problem:

• Given with three text files, count the


total of all the numerical elements from
these files.
• file1 file2 file3
Problem:

• Given the following file, count average


of the marks find the name of topper
student.
data.txt
Closing a fi le

• When we are done with operations to the


file, we need to properly close the file.
• Closing a file will free up the resources that
were tied with the file and is done using
Python close() method.
• Python has a garbage collector to clean up
unreferenced objects but, we must not rely
on it to close the file.
f = open("test.txt")
# perform file operations
f.close()
Writing the file

• In order t o write into a fi le in Pyt hon,


we need to open it in write 'w',
append 'a' or exclusive creation 'x'
mode.
• We need t o be careful wit h t he 'w' mode
as it will overwrite into the file if it
already exists. All previous data are
erased.
• Writing a string or sequence of byt es (for
binary files) is done using write()
method. This method returns the
number of characters written to the file.
Example:

with open("test.txt",'w') as f:
f.write("my first file\n")
f.write("This file\n\n")
f.write("contains three lines\n")
File writing methods

• writable()
– Returns True if the file stream can be
written to.
• write(s)
– Write string s to the file and
return the number of characters
written.
• writelines(lines)
– Write a list of lines t o t he fi le.
Sample File copy operation

You might also like