Practical8 Python Programming Jeb6qONhW8
Practical8 Python Programming Jeb6qONhW8
Practical8 Python Programming Jeb6qONhW8
PRACTICAL 8
Part A (To be referred by students)
Understand and apply the various file handling operations to implement following programs
1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
Theory:
Data stored in variables and lists is temporary — it’s lost when the program terminates.
Python allows a program to read data from a file or write data to a file. Once the data is saved
in a file on computer disk, it will remain there after the program stops running. The data can
then be retrieved and used at a later time.
There are two types of files in Python - text files and binary files.
A text file is processed as a sequence of characters. In a text file there is a special
end-of-line symbol that divides file into lines. In addition, you can think that there is a
special end-of-file symbol that follows the last component in a file. A big advantage
of text files is that it may be opened and viewed in a text editor such as Notepad.
A binary file stores data that has not been translated into character form. Binary files
typically use the same bit patterns to represent data as those used to represent the data
in the computer's main memory. These files are called binary files because the only
thing they have in common is that they store the data as sequences of zeros and ones.
Opening a File
- Open()
- Fileheader =open(‘file name’,”open mode”,”buffering”)
- Various file opening modes
o w
o r
o a
- Example:-
F=open(“myfile.txt”,”w”)
2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
Closing File:-
f.close( )
Example:- Writing Contents to file:
f=open('myfile.txt','w')
str=input('Enter text to write to file')
f.write(str)
f.close()
f.close()
3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
sys.exit()
print('File contents are')
str=f.read()
print(str)
f.close()
Method Description
read() Returns the file content.
readline() Read single line
readlines() Read file into a list
truncate(size) Resizes the file to a specified size.
write() Writes the specified string to the file.
writelines() Writes a list of strings to the file.
close() Closes the opened file.
seek() Set file pointer position in a file
tell() Returns the current file location.
Returns a number that represents the stream, from the operating
fileno()
system's perspective.
flush() Flushes the internal buffer.
4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
Reading a File:
In order to read a file in python, we must open the file in read mode.
There are three ways in which we can read the files in python.
read([n])
readline([n])
readlines()
Here, n is the number of bytes to be read.
# Reading the entire file at once
filename = “C:/Documents/Python/test.txt”
filehandle = open(filename, ‘r’)
filedata = filehandle.read()
print(filedata)
--OR—
with open("large_file.txt", "r") as f:
print(f.read())
# Using this function we can read the content of the file on a line by line basis
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline())
--OR—
with open("large_file.txt", "r") as f:
print(f.readline())
# reading all the lines present inside the text file including the newline characters and
# display as a list
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readlines())
5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
We need to be very careful while writing data into the file as it overwrites the content present
inside the file that you are writing, and all the previous data will be erased.
Append to File:
To append data into a file we must open the file in ‘a+’ mode so that we will have access to
both the append as well as write modes.
The tell() method returns the current file position in a file stream. Syntax:
file_object.tell()
The seek() method sets the current file position in a file stream and returns the new position.
Syntax:
file_object.seek(offset [, reference_point])
where offset means how many positions you will move; reference_point defines your point of
reference:
0: means your reference point is the beginning of the file
1: means your reference point is the current file position
2: means your reference point is the end of the file
6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
infile = open("testfile.txt","r")
f = open("sample.txt", "r")
f = open("sample.txt", "r")
# move to 11 character
f.seek(11)
7|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering/ School of Technology
Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II
PRACTICAL 8
Part B (to be completed by students)
8|Page