Text File
Text File
Text Files
INTRODUCTION TO FILES
The key function for working with files in Python is the open()
function.
●It accepts two arguments : filename, and mode.
Syntax:
<file_object_name> = open(<file_name>,<mode>)
Example: f = open(“demo.txt”,”r”)
●Can specify if the file should be handled as binary or text Mode :
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
Govindaraja K, Sainik School Kodagu 4
OPENING A TEXT FILE
File Objects:
It serves as a link to file residing in your computer.
It is a reference to the file on the disk and it is through
this link python program can perform operations on
the files.
File access modes:
It governs the type of operations(such as read or write
or append) possible in the opened file.
Govindaraja K, Sainik School Kodagu 5
Mode Description
close()- method will free up all the system resources used by the file,
this means that once file is closed, we will not be able to use the file
object any more.
<fileobject>. close() will be used to close the file object, once we have
finished working on it.
Syntax:
<fileObject>.close()
Example : f.close()
●It is important to close your files, as in some cases, due to buffering,
changes made to a file may not show until you close the file.
Govindaraja K, Sainik School Kodagu 7
READING FROM A FILE
By default the read() method returns the whole text, but you can also specify how
many characters you want to return by passing the size as argument.
Syntax: <file_object>.read([n])
where n is the size
●To read entire file : <file_object>.read()
●To reads only a part of the File : <file_object>.read(size)
f = open("demo.txt", "r")
print(f.read(15))
fin=open(r"C:\Users\user-pc\OneDrive\Desktop\marks.txt", "r")
print(fin.read())
fin.close()
10
READLINE() METHOD
Syntax:
<file_object>.readline()
●Example
f = open("demo.txt", "r")
print(f.readline())
●This example program will return the first line in the file
“demo.txt” irrespective of number of lines inGovindaraja
the text file.
K, Sainik School Kodagu 11
READING A
COMPLETE FILE LINE
BY LINE USING
READLINE().
Syntax:
<file_object>.readlines()
●It returns a list, which can then be used for manipulation.
print(f.readlines())
Govindaraja K, Sainik School Kodagu 13
Read a file using Readlines()
14
15
WRITING TO A TEXT FILE
f = open("demo_write.txt", “w")
f.write("Hello students \n We are learning data file
handling…..!")
f.close()
f = open("demo_write.txt", "r") #open and read the file
after the writting
print(f.read()) Govindaraja K, Sainik School Kodagu 18
Govindaraja K, Sainik School Kodagu 19
WRITELINES() METHOD
Open the marks.txt" and moving the file cursor in the file:
print("Learning to move the file object")
fin=open(r"C:\Users\govin\OneDrive\Desktop\marks.txt", "r+")
print(fin.read(5))
position=fin.tell()
print("The position of the file object is",position)
fin.seek(0)
print("Now the file object is at the beginning of the file:",fin.tell())
fin.seek(10)
print("We are moving to 10th byte position from the beginning of file")
print("The position of the file object is at", fin.tell())
str=fin.read()
print(str)
fin.close() Govindaraja K, Sainik School Kodagu 27
THE FLUSH()
32
UNPICKLING is used to convert the byte
stream back to the original
structure while reading the contents of
the file.
33
Before reading
or writing to a
It provides two
file, we must import pickle
main methods :
import the
pickle module.
dump() load()
method method
PICKLE MODULE
34
Opening a binary file:
Like text file except that it should
be opened in binary mode.
Adding a ‘b’ to the text file mode
OPENING makes it binary - file mode.
BINARY Syntax:
FILES
Fileobject=open(“filename’,
“rb”)
EXAMPLE :
f=
open(“demo.dat”,”rb”) 35
Closing a binary file
CLOSING
BINARY fileobject . close()
FILES
36
Mode Description
39
pickle.load() method is used to
read data from a file
Syntax :
PICKLE.LOA <structure> =
D() METHOD pickle.load(<FileObject>)
Structure can be any sequence in
Python such as list, dictionary etc.
FileObject is the file handle of file
in which we have to write.
40
# Demonstration of dump() in Python
import pickle
print("Demonstration of demp() method")
fin=open(r"C:\Users\govin\OneDrive\Desktop\dump1.data","rb")
city=pickle.load(fin)
print(city)
fin.close()
41
PICKLING
AND
UNPICKLING
42
Thank you
43
Govindaraja K, Sainik School Kodagu