Exp 6 IPP
Exp 6 IPP
6 File Handling
a. Read all the content present inside the file
i. Read only the first 5 characters of the
file.
ii. Read the content of the file on a line by
line basis.
iii. Read all the lines present inside the text
file including the newline characters.
iv. ReadaspecificlinefromaFile
b. Write content in a file
i. Write the String ‘Hello World’ into the
‘test.txt’ file
ii. Write “Hello World” in first line and
“Hello Python” in second line
iii. Write list of fruits in .txt file
d. Write a program to read resume and its details
7
8
9
10
11
* Students are expected to be ready with the prerequisite before attending the lab
Experiment No.06
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Experiment 6
A.1 Aim: File Handling
a. Read all the content present inside the file
v. Read only the first 5 characters of the file.
vi. Read the content of the file on a line by line basis.
vii. Read all the lines present inside the text file including the newline characters.
viii. ReadaspecificlinefromaFile
b. Write content in a file
iv. Write the String ‘Hello World’ into the ‘test.txt’ file
v. Write “Hello World” in first line and “Hello Python” in second line
vi. Write list of fruits in .txt file
c. Write a program to read resume and its details
A.2 Prerequisite:
Reading and writing from different types of files
A.3 Learning Outcome:
After completing this exercise you will be able to-
1. Develop a small application to read content of a file
2. Implement a program to write applications in the file
A.4 Theory:
In Python, files are treated in two modes as text or binary. The file may be in the text or binary
format, and each line of a file is ended with the special character.
o Open a file
o Read or write - Performing operation
o Close the file
file object = open(<file-name>, <access-mode>)
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
f = open("demofile.txt", "r")
print(f.read())
f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())
f = open("demofile.txt", "r")
print(f.read(5))
Read Lines
f = open("demofile.txt", "r")
print(f.readline())
By looping through the lines of the file, you can read the whole file, line by line:
f= open("demofile.txt", "r")
for x in f:
print(x)
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
To create a new file in Python, use the open() method, with one of the following parameters:
"x" - Create - will create a file, returns an error if the file exist
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
f = open("myfile.txt", "x")
***************************************************************************
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the MS Teams assignment link at the end of the practical)
Roll No. L012 Name: Shreyas Desai
Program : CSDS 311 Division: L
Batch: B1 Date of Experiment:
Date of Submission: Grade :
2:
3:
B.3 Conclusion:
(Students must write the conclusive statements as per the actual attainment of individual
outcomes listed above and learning/observation noted in section B.2)
I was able to develop a small application to read content of a file
I could implement a program to write applications in the file
Q1. To read all contents from file object FILE at once we may use
a) FILE.read(*)
b) FILE.readlines()
c) FILE.read()
d) FILE.readline()
Q2. To read 20 characters from file object FILE at once we may use
a) FILE.read(20)
b) FILE.readlines(20)
c) FILE.read(char=20)
d) FILE.readline(char=20)
Q3. Which of the following file mode will refer to the BINARY mode?
a) binary
b) b
c) bin
d) w
Q4. Which of the following function is used to write LIST OF STRINGS in a file?
a) write()
b) writeline()
c) writelines()
d) write(all)
Q5 If we want to add more contents in an existing file, file must be opened in............mode
a) binary
b) append
c) write
d) it is not possible