What method is used to read the entire content of a file in Python?
The read() method.
True or False: The read() method can take an argument specifying the number of bytes to read.
True.
How do you open a file for reading in Python?
Use the open() function with ‘r’ mode.
What is the default mode for the open() function?
‘r’ (read mode).
Fill in the blank: To write to a file, you must open it in _____ mode.
‘w’ (write mode).
What method is used to read a CSV file in Python?
The csv.reader() method.
What library do you need to import to work with CSV files in Python?
The csv library.
True or False: The json library in Python is used to handle JSON data.
True.
What method is used to convert a Python object to a JSON string?
The json.dumps() method.
How do you write a JSON object to a file in Python?
Use json.dump() method.
What method is used to read a JSON file in Python?
The json.load() method.
What function is used to open a file in write mode and create it if it does not exist?
The open() function with ‘w’ mode.
Fill in the blank: The _____ method is used to write a row to a CSV file.
writerow()
What does the writeheader() method do in the csv module?
It writes the header row to the CSV file.
Which method would you use to read a specific number of lines from a file?
The readline() method.
True or False: The write() method can only append data to a file.
False.
What does the ‘a’ mode do when opening a file?
It opens the file for appending data.
What method would you use to read all lines from a file into a list?
The readlines() method.
How do you ensure that a file is properly closed after opening it?
Use a with statement.
True or False: You can use the csv.DictReader() to read CSV files into a dictionary format.
True.
What is the purpose of the newline parameter in the open() function?
It controls how universal newlines work.
Fill in the blank: The _____ method is used to write a string to a file.
write()
What Python function would you use to check if a file exists before opening it?
The os.path.exists() function.
What is the difference between read() and readlines()?
read() returns the entire file as a string, while readlines() returns a list of lines.