File Handling
File Handling
Vinay Agrawal
ASST. PROFESSOR
CEA DEPT.
GLA UNIVERSITY,
MATHURA
File Operations
1. open()
Example
2. close()
3. write()
4. read()
Other functions
To read contents from file
1. fp.read(n) :- n is the number of bytes
to read
2. fp.readline():- It will read only one
line that is point by fp.
3. fp.readlines(): It will all the lines of
the file and store all the lines in list
To write contents into the file
1. fp.writelines(l):- It will write all the
lines present in the list l
Programs
1. WAP to write starting 10 even numbers in
the output file output1.txt ( in single line)
2. WAP to write starting 10 odd numbers in
the output file output2.txt ( in multiple lines)
3. WAP to write 5 random numbers between
50-60(both inclusive) in the output file
output3.txt ( in single line)
4. WAP that reads data from a file and calculates
the percentage of vowels and consonants in
the file
Prgrams
5. WAP that accepts filename as an input
from the user. Open the file and count
the number of times a character(I/P from
user) appears in the file
6. WAP that copies one Python Script into
another in such a way that all comment
lines are skipped and not copied in the
destination file.
7. WAP to enter the name of the file and
print the longest line present in the file.
8. Write a Python program to read a file
(“input.txt”) of oil prices in England ,
Australia and India. Output the average oil
price of each country to an output file
output.txt.(Note: All fields are separated
with one space)
Modes of opening the file
Text File Binary File
r rb
w wb
a ab
r+ rb+
w+ wb+
a+ ab+
x xb
with keyword
We can open the file using with keyword.
Example:
with open(“a.txt“,”r”)
s=fp.read()
as fp:
s=fp.read()
print(s)
The advantage of using with keyword is “File
is properly closed after it is used even if an
error occurs during read or write operation or
even when you forget to explicitly close the
file”
Other useful File methods
fp.fileno() :- It returns the file number of
the file (File Descriptor)
fp.truncate(n) :- Resizes the file to n
bytes
fp.tell():- It tells the current position of
the file pointer
fp.seek(byteoffset,reference) :-It is used
to move the file pointer fp to a new
location.
Assignment
1. WAP that reads a file line by line. Each line read from the
file is copied to another file with line numbers specified at
the beginning of the line.
For example, if contents of file is as follows:
Hello
Bye
GLA
Expected O/P:
1. Hello
2. Bye
3. GLA
2. WAP that counts the number of tabs, spaces
and newline characters in a file
3. WAP that counts the number of characters,
words and lines in a file
4. WAP that generates a Quiz and uses two
files-Questions.txt and Answers.txt . The
program opens Questions.txt and reads a
question and displays the question with
options on the screen. The program then
opens the Answers.txt file and displays the
correct answers.
5. WAP to read first n lines from the file.
6. WAP to read last n lines from the file.
7. WAP to read from m line to n line.
8. WAP to write all the prime numbers
from 2 to 100 to the output file
output1.txt
9. WAP to append all the armstrong
numbers from 100 to 999 to the above
file outfile1.txt
10. WAP to write only those lines in the output file that starts with 172
Input File
Hello Raj
172.16.42.35
165.72.42.1
127.0.01
172.12.12.10
17.13.11.10
Expected O/P:
172.16.42.35
172.12.12.10