Python File Handling Practice Questions
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.
Try b
ut never cry
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