NEW
Font size
WorksheetsMastering Python File Handling
Total questions: 15
Worksheet time: 8mins
What is the purpose of the 'open' function in Python?
To create a new file and write data to it.
To close a file and release system resources.
To open a file and return a file object for reading or writing.
To read data from a network socket.
How do you read the entire content of a file in Python?
file_content = readfile('filename.txt')
file_content = open('filename.txt', 'r').read()
file_content = open('filename.txt').readlines()
file_content = read('filename.txt')
What mode would you use to write to a file, overwriting its contents?
r
a+
w
a
Explain the difference between 'read()', 'readline()', and 'readlines()'.
The difference is that 'read()' returns the whole file as a string, 'readline()' returns one line at a time as a string, and 'readlines()' returns a list of all lines.
'read()' reads one line at a time, 'readline()' reads the whole file, and 'readlines()' returns a string.
'read()' returns a list of strings, 'readline()' returns the whole file, and 'readlines()' returns one line.
'read()' returns a list of all lines, 'readline()' returns the whole file, and 'readlines()' returns one line.
How can you append data to an existing file in Python?
Use open('filename', 'w') to overwrite an existing file.
Use open('filename', 'a') to append data to an existing file.
Use open('filename', 'x') to create a new file only.
Use open('filename', 'r') to read data from a file.
What is the significance of using 'with' when opening a file?
It prevents any errors from occurring while writing.
It ensures proper resource management by automatically closing the file.
It allows for faster file access during reading.
It enables multiple files to be opened simultaneously.
How do you handle exceptions when working with file operations?
Ignore exceptions and continue processing files.
Log errors to a file without handling them.
Use try-except blocks to catch and handle exceptions during file operations.
Always delete the file if an error occurs.
What method would you use to write a list of strings to a file?
Use the 'write()' method to append strings to the file.
Employ the 'save()' method to store strings in a database.
Use the 'writelines()' method after opening the file in write mode.
Utilize the 'print()' function to display strings on the console.
How can you check if a file exists before attempting to open it?
Use 'os.file_check(file_path)' in Python to verify file existence.
Use 'file.exists(file_path)' in Java to check for a file.
Use 'os.path.exists(file_path)' in Python to check if a file exists.
Check if 'file_path' is not empty before opening it.
What is the purpose of the 'close' method in file handling?
To create a new file and write data to it.
To close an open file and release resources.
To delete a file from the system permanently.
To read data from a file and store it.
How do you read a file line by line in a loop?
with file('filename.txt') as f:
with open('filename.txt', 'r') as file: for line in file: print(line.strip())
for line in read(file):
open('filename.txt') as file:
What is the difference between binary and text file modes?
Text files can only contain numbers and symbols.
Binary files store data in raw format; text files store data as readable characters.
Binary files are easier to read than text files.
Binary files are always larger than text files.
How can you create a new file if it does not exist when opening it?
Use 'open("filename.txt", "b")' for binary mode.
Use 'open("filename.txt", "w")' or 'open("filename.txt", "a")' in Python.
Use 'open("filename.txt", "r")' to read the file.
Use 'open("filename.txt", "x")' to create a new file.
What is the function of 'seek()' in file handling?
The 'seek()' function changes the current position of the file pointer.
The 'seek()' function closes the file after use.
The 'seek()' function reads data from the file.
The 'seek()' function writes data to the file.
How can you read a specific number of bytes from a file?
Invoke file.read(n, m) to read 'n' bytes with a buffer size of 'm'.
Call file.readBytes(n) to access 'n' bytes from a file.
Use file.get(n) to retrieve 'n' bytes from a file.
Use file.read(n) to read 'n' bytes from a file.
