40+ Important Python File Handling Practice Questions

Share with others

Table of Content

  1. Python File Handling Practice Questions – Test 1
  2. Python File Handling Practice Questions – Test 2
  3. Python File Handling Practice Questions – Test 3
  4. Python File Handling Practice Questions – Test 4
Python File Handling
thon File Handling

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?

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”

Python File Handling Practice Questions – Test 2

Q1. f = open(“data.txt”)

  1. Identify name of file.
  2. Identify name of function.
  3. What if ‘f’ in above code?
  4. 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.

  1. Read Only
  2. Write only
  3. Read and Write
  4. Write and Read

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

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”) 

File Handling Practice Questions

Q3. Write a program in python to read first line from the file(“data.txt”)

File Handling Practice Questions

Q4. Write a program in python to display number of lines in a file(“data.txt”). 

File Handling Practice Questions

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 or if any code of above is not clear to you , share your valuable feedback on csiplearninghub@gmail.com

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


Share with others

Leave a Reply

%d bloggers like this: