Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
91 views

Python File Handling Practice Questions

Uploaded by

Khushee Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Python File Handling Practice Questions

Uploaded by

Khushee Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python File Handling Practice Questions –

Test 1
Q1. Name two types of data files available in python .
Hide Answer
Ans. Text File and Binary File
Q2. In text file each line is terminated by a special character called _______
Hide Answer
Ans. EOL (End of Line)
Q3. In python, default EOL character is _______
Hide Answer
Ans. \n
Q4. Is there any delimiter of line in binary File?
Hide Answer
Ans. No
Q5. In binary files, data stored in the same format as it is stored in computer
memory. (T/F)
Hide Answer
Ans. True
Q6. Processing of binary file is faster than text file (T/F)
Hide Answer
Ans. True
Q7. Writing data is same as appending data in file. (T/F)
Hide Answer
Ans. False
Q8. Can we read file without opening. (T/F)
Hide Answer
Ans. False
Q9. Which function is used to open a file?
Hide Answer
Ans. open()
Q10. Write the code in python to open a file named “try.txt”
Hide Answer
Ans. f = open(“try.txt”)
Q11. Write one basic difference between Text file and Binary file in Python.
Hide Answer
Ans. A text file consists of human readable characters, which can be opened
by any text editor. while, binary files consist of non-human readable
characters and symbols, which require specific programs to access its
contents.

Python File Handling Practice Questions –


Test 2
Q1. f = open(“data.txt”)
Consider the code given above and write the answers of the following:
1. Identify name of the file.
2. Identify name of the function used above.
3. What is ‘f’ in above code?
4. The above statement will __________ file in ________ mode.
Hide Answer
Ans.
1. data.txt
2. open()
3. file object
4. open, read
Q2. What is the default mode of opening a file?
Hide Answer
Ans. read mode

Q3. Write a statement in python to open a file named “data.txt” stored in


folder “content” in D drive.
Hide Answer
Ans. f = open(“d:\\content\\data.txt”)
OR
f = open(r, “d:\content\data.txt”)
Q4. What error is returned by following statement, if file “try.txt” does not
exist?
f = open(“try.txt”)
Hide Answer
Ans. FileNotFoundError
Q5. Name the three modes in which file can be opened in python.
Hide Answer
Ans. Read, Write and Append
Q6. What is the purpose of ‘r’ as prefix in the given statement?
f = open(r, “d:\color\flower.txt”)
Hide Answer
Ans. ‘r’ makes the string as raw string, means there is no special character in
string.
Q7. What do you mean by file object in python?
Hide Answer
Ans. File object is
reference to a file. It is used to read and write data to a file.
Q8. Another name of file object is _______
Hide Answer
Ans. File Handle
Q9. Write the symbols used in text file mode for the following operations.
1. Read Only
2. Write only
3. Read and Write
4. Write and Read
Hide Answer
Ans.
1. r
2. w
3. r+
4. w+
Q10. Write the symbols used in binary file mode for the following
operations.
1. Read Only
2. Write only
3. Read and Write
4. Write and Read
Hide Answer
Ans.
1. rb
2. wb
3. rb+
4. wb+
Python File Handling Practice Questions –
Test 3
Q1. What is the difference between r+ and w+ mode?
Hide Answer
Ans. r+ mode will return error if file does not exist while w+ mode will
create a new file if file does not exist.
Q2. What is the difference between write and append mode?
Hide Answer
Ans. Write mode over writes the existing data while append mode add the
new data at the end of the file.
Q3. Which method is used to close the file in python?
Hide Answer
Ans. close()
Q4. Write the statement to close the file “test.txt” which is associated with
file object named “fo”.
Hide Answer
Ans. fo.close()
Q5. Write one similarity and one difference between a+ and w+.
Hide Answer
Ans. Similarity : In both the modes, we can do read and write operations.
Difference : In w+ mode file will be truncated (previous data lost) while in
a+ mode, file’s existing data will not be deleted and new data will be added
at the end of the file.
Q6. What is the difference between read() and read(n) functions?
Hide Answer
Ans. read() : will read entire data from the file.
read(n) : will read first n characters/bytes from the file.
Q7. What is the difference between readline() and readlines() ?
Hide Answer
Ans. readline() :This function will read one line from the file.
readlines() : This function will read all the lines from the files.
Q8. Write a program to read first 10 characters from a file named “data.txt”
Ans.
f = open(“data.txt”,”r”)
data = f.read(10)
print(data)
Q9. Write a program to read entire content from the file named “test.txt”
Hide Answer
Ans.
f = open(“test.txt, “r”)
d = f.read()
print(d)
Q10. Write the output of the following, if the data stored in file “try.txt” is
given below. (“f” is the file object associated with file)
Try Try but never cry
print(f.read(4))
print(f.read(5))
print(f.read())
Hide Answer
Ans.
Try

Try b

ut never cry

Python File Handling Practice Questions –


Test 4
Q1. Write a program in python to read entire content of file (“data.txt”)
Hide Answer
Ans.
f = open("data.txt","r")
l = f.read()
print(l)

Q7. Name two functions which are used to write data into file.
Hide Answer
Ans. write() and writelines()
Q8. Accept five names from the user and write in a file “name.txt”
Hide Answer
Ans.
f = open("name.txt","w")
for i in range(5):
n = input("Enter name")
f.write(n)
f.close()
Q9. Accept five sports names from the user and write in a file “sport.txt”
(each name should write in separate line)
Hide Answer
Ans
f = open("name.txt","w")
for i in range(5):
n = input("Enter sport name")
f.write(n)
f.write('\n')
f.close()
Q10. Accept five hobbies from the user and write in a file “hobby.txt” (each
hobby should write in separate line) without using write() function.
Hide Answer
Ans.
f = open("hobby.txt","w")
h=[]
for i in range(5):
n = input("Enter hobby name")
h.append(n,'\n')
f.writelines(h)
f.close()
Disclaimer : I tried to give the correct code of all the above important
questions of Python File handling, but there may be some typing error in
above Python File Handling article or if any code given above is not clear to
you , share your valuable feedback on csiplearninghub@gmail.com

You might also like