Python File Handling Practice Questions
Table of Content
- Python File Handling Practice Questions – Test 1
- Python File Handling Practice Questions – Test 2
- Python File Handling Practice Questions – Test 3
- Python File Handling Practice Questions – Test 4
![Python File Handling](https://i0.wp.com/csiplearninghub.com/wp-content/uploads/2020/10/logo.png?resize=183%2C52&ssl=1)
Python File Handling Practice Questions – Test 1
Q1. Name two types of data files available in python .
Q2. In text file each line is terminated by a special character called _______
Q3. In python, default EOL character is _______
Q4. Is there any delimiter of line in binary File?
Q5. In binary files, data stored in the same format as it is stored in computer memory. (T/F)
Q6. Processing of binary file is faster than text file (T/F)
Q7. Writing data is same as appending data in file. (T/F)
Q8. Can we read file without opening. (T/F)
Q9. Which function is used to open a file?
Q10. Write the code in python to open a file named “try.txt”
Q11. Write one basic difference between Text file and Binary file in Python.
Python File Handling Practice Questions – Test 2
Q1. f = open(“data.txt”)
Consider the code given above and write the answers of the following:
- Identify name of the file.
- Identify name of the function used above.
- What is ‘f’ in above code?
- The above statement will __________ file in ________ mode.
Q2. What is the default mode of opening a file?
Q3. Write a statement in python to open a file named “data.txt” stored in folder “content” in D drive.
Q4. What error is returned by following statement, if file “try.txt” does not exist?
f = open(“try.txt”)
Q5. Name the three modes in which file can be opened in python.
Q6. What is the purpose of ‘r’ as prefix in the given statement?
f = open(r, “d:\color\flower.txt”)
Q7. What do you mean by file object in python?
Q8. Another name of file object is _______
Q9. Write the symbols used in text file mode for the following operations.
- Read Only
- Write only
- Read and Write
- Write and Read
Q10. Write the symbols used in binary file mode for the following operations.
- Read Only
- Write only
- Read and Write
- Write and Read
Python File Handling Practice Questions – Test 3
Q1. What is the difference between r+ and w+ mode?
Q2. What is the difference between write and append mode?
Q3. Which method is used to close the file in python?
Q4. Write the statement to close the file “test.txt” which is associated with file object named “fo”.
Q5. Write one similarity and one difference between a+ and w+.
Q6. What is the difference between read() and read(n) functions?
Q7. What is the difference between readline() and readlines() ?
Q8. Write a program to read first 10 characters from a file named “data.txt”
Q9. Write a program to read entire content from the file named “test.txt”
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())
Python File Handling Practice Questions – Test 4
Q1. Write a program in python to read entire content of file (“data.txt”)
Q2. Write a program in python to read first 5 characters from the file(“data.txt”)
Q3. Write a program in python to read first line from the file(“data.txt”)
Q4. Write a program in python to display number of lines in a file(“data.txt”).
Q5. Write a program in python to display first line from the file(“data.txt”) using readlines().
Q6. readlines() function returns the entire data in the form of list of _____
Q7. Name two functions which are used to write data into file.
Q8. Accept five names from the user and write in a file “name.txt”
Q9. Accept five sports names from the user and write in a file “sport.txt” (each name should write in separate line)
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.
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
Python File Handling Practice Questions
Python File Handling Practice Questions
Related Post
10 Important Questions of Binary File Handling in Python
10 Important Questions of Text File Handling in Python
Handout of Text File Handling in Python
Handout of Binary File Handling in Python
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series