0% found this document useful (0 votes)
3 views17 pages

Python File I/O Operations Guide

The document provides an overview of file input and output operations in Python, detailing how to open, write, and read files. It explains the use of different modes for file operations, such as writing and appending, and introduces methods like read(), readline(), and readlines() for reading file content. Additionally, it includes examples and references for further learning on the topic.

Uploaded by

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

Python File I/O Operations Guide

The document provides an overview of file input and output operations in Python, detailing how to open, write, and read files. It explains the use of different modes for file operations, such as writing and appending, and introduces methods like read(), readline(), and readlines() for reading file content. Additionally, it includes examples and references for further learning on the topic.

Uploaded by

Ragavan Murali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Files I/O in python

Jeya Pandian. M
APII/CSE
Srinivasa Ramanujan Centre
SASTRA

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC


Files Input / Output
Files
 Files are named locations on disk to store related
information. They are 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 the computer is turned off), we use files
for future use of the data by permanently storing them.
 In Python, a file operation takes place in the following order:

 Open a file

 Read or write (perform operation)

 Close the file

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 2


File Types

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 3


How to open and close files

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 4


How to write a file
outf= open("[Link]", 'w‘)
[Link]("my first file\n")
[Link]("This file\n\n")
[Link]("contains three lines\n")
[Link]() #manual file close
 Normally file characters are encoded in ASCII format.

 We can also use encoding = 'utf-8‘ to encode in Unicode

format:
outf= open("[Link]",'w‘, encoding = 'utf-8‘)

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 5


How to write a file (with .. as)
 When we want to close the file automatically after
operations we can use with.. as statement.
with open("[Link]", 'w’) as f:
[Link]("my first file\n")
[Link]("This file\n\n")
[Link]("contains three lines\n")

File content will be:


my first file
This file

contains three lines

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 6


How to write a file in append
mode
 When we want to append the line to a file with pre-exist
content we can use with.. as statement.
 Assume that you have ‘[Link]’ with some content.

with open("[Link]", ‘a’) as f:


[Link]("This line is appended at last\n ")

File content will be:


my first file
This file

contains three lines


This line is appended at last

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 7


How to read a file
 To read a file in Python, we must open the file in reading
‘r’ mode.

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 8


How to read a file
read([size])
 Read the content specified file .

 We can use the read(size) method to read in the size

number of data.
 If the size parameter is not specified, it reads and returns up
to the end of the file.
 We can read the [Link] file we wrote in the above section
in the following way:
>>> f = open("[Link]",'r’)
>>> [Link](4) # read the first 4 data 'This'
or
>>> [Link]() # read till end of file

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 9


How to read a file
readline()
 We can also use readine() function to read the files line by

line.
 We can read the [Link] file we wrote in the above section

in the following way:


>>> f = open("[Link]",'r’)
>>> print([Link]())
>>> print([Link]())
>>> print([Link]())

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 10


How to read a file
readlines()
 We can also use readlines() function to read the files as a

list of lines.
 Reads and returns a list of lines from the file.

 We can read the [Link] file we wrote in the above section

in the following way:


>>> f = open("[Link]",'r’)
>>>ls=[]
>>>ls=[Link]()
>>> print(ls[0],ls[1],ls[2])

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 11


Example: read a file

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 12


Work with lists in text file:
How to read & write list of strings

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 13


Work with lists in text file:
How to read & write list of numbers

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 14


Example : Use of for

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 15


Try

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 16


References
 Textbook: “Python programming - beginner to pro”
by Michael Urban, Joel Murach,2016.
 Mark Lutz, Learning Python, O’Reily Media, Fifth
Edition, 2013.

CSE304-Python Programming with Web Frameworks Jeya Pandian.M/AP/CSE/SRC 17

You might also like