WorksheetsMastering Python File Handling
Total questions: 15
Worksheet time: 8mins
What function is used to read the entire content of a file in Python?
get_content()
read()
fetch()
load()
How do you read a single line from a file using Python?
line = open('filename.txt').read()
line = open('filename.txt').readline(0)
line = open('filename.txt').readlines()
line = open('filename.txt').readline()
What method would you use to read all lines from a file into a list?
file.getlines()
file.readlines()
file.fetch_all()
file.read()
Explain the difference between read() and readline() in Python file handling.
read() reads the whole file at once; readline() reads one line at a time.
read() is used for binary files; readline() is used for text files.
read() reads one line at a time; readline() reads the whole file at once.
read() reads a specified number of bytes; readline() reads the entire file.
What is the purpose of the readlines() function?
To read all lines from a file and return them as a list.
To read a single line from a file.
To write all lines to a file.
To count the number of lines in a file.
How can you write a string to a file in Python?
open('filename.txt', 'a').write('Your string here')
open('filename.txt', 'w').write('Your string here')
write('Your string here', 'filename.txt')
open('filename.txt', 'r').write('Your string here')
What method would you use to write multiple lines to a file at once?
Use the 'append()' method.
Use the 'flush()' method.
Use the 'writelines()' method.
Use the 'write()' method.
How do you append data to an existing file in Python?
Use open('filename', 'r') to append data to a file.
Use open('filename', 'w') to append data to a file.
Use open('filename', 'a') to append data to a file.
Use open('filename', 'x') to append data to a file.
What does the seek() function do in file handling?
The seek() function closes the file.
The seek() function reads data from a file.
The seek() function writes data to a file.
The seek() function changes the current file position.
How can you move the file pointer to the beginning of a file?
Use the read method to access the file.
Close the file and reopen it.
Use the tell method to check the position.
Use the seek method with an offset of 0.
What is the default mode for opening a file in Python?
a
x
w
r
How do you ensure that a file is properly closed after operations?
Use a context manager or ensure to close the file in a finally block.
Close the file only if an error occurs.
Use a global variable to track the file state.
Leave the file open until the program ends.
What happens if you try to read from a file that has been opened in write mode?
Reading from a file opened in write mode will result in an error.
The content will be written to the file instead of being read.
The file will be opened in read mode automatically.
The file will be read successfully without any issues.
How can you read a specific number of characters from a file?
Use 'file.get(n)' to retrieve characters.
Read the entire file and then slice the string.
Use 'file.readline(n)' to read a specific number of characters.
Use 'file.read(n)' after opening the file.
What is the significance of the 'with' statement when handling files in Python?
The 'with' statement is used to create new files in Python.
The 'with' statement simplifies file handling by ensuring proper resource management.
The 'with' statement allows for multiple files to be opened simultaneously without any issues.
The 'with' statement is only necessary for reading files, not writing them.
