Vidyalelo
Python · all questions

Files Handling in Python
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

80

Questions

3/4

Page

Pick an option on a question to see the right answer and solution.

What does the w mode indicate when opening a file in Python?

Select an option to see the answer and solution.

Which of the following is not a valid mode to open a file?

Select an option to see the answer and solution.

How can you copy the contents of one file to another in Python?

Select an option to see the answer and solution.

Which one of the following is not attributes of file?

Select an option to see the answer and solution.

What is the use of "w" in file handling?

Select an option to see the answer and solution.

What will be the output of the following Python code?
str = raw_input("Enter your input: ");
print "Received input is : ", str

Select an option to see the answer and solution.

What will be the output of the following Python code?
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
 
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
 
for index in range(5):
   line = fo.next()
   print "Line No %d - %s" % (index, line)
 
# Close opened file
fo.close()

Select an option to see the answer and solution.

What will be the output of the following Python code?
f = None
for i in range (5):
    with open("data.txt", "w") as f:
        if i > 2:
            break
print(f.closed)

Select an option to see the answer and solution.

How do you rename a file?

Select an option to see the answer and solution.

How can you read the entire contents of a binary file in Python?

Select an option to see the answer and solution.

What will be the output of the following Python code?
str = input("Enter your input: ");
print "Received input is : ", str

Select an option to see the answer and solution.

What is unpickling?

Select an option to see the answer and solution.

The readlines() method returns . . . . . . . .

Select an option to see the answer and solution.

What is the output of the following code:
with open('data.txt', 'r') as file:
lines = file.readlines()
print(len(lines))

Select an option to see the answer and solution.

To read the next line of the file from a file object infile, we use . . . . . . . .

Select an option to see the answer and solution.

How can you rename a file in Python?

Select an option to see the answer and solution.

What is the pickling?

Select an option to see the answer and solution.

What is the output of the following code:
with open('data.txt', 'r') as file:
content = file.read(10)
print(content)

Select an option to see the answer and solution.

How do you close a file object (fp)?

Select an option to see the answer and solution.

What is the use of seek() method in files?

Select an option to see the answer and solution.