Complete the code to open a file safely using a context manager.
with open('example.txt', [1]) as file: content = file.read()
The mode 'r' opens the file for reading, which is safe for reading content.
Complete the code to ensure a file is closed properly after writing.
file = open('output.txt', [1]) file.write('Hello') file.close()
The mode 'w' opens the file for writing and overwrites existing content.
Fix the error in the code to properly handle file opening with a context manager.
file = open('data.txt', 'r') with [1] as f: data = f.read()
The variable 'file' is already the opened file object and should be used in the with statement.
Fill both blanks to create a dictionary comprehension that stores file sizes for files larger than 1000 bytes.
sizes = {filename: [1] for filename, size in files.items() if size [2] 1000}The comprehension stores the size for each filename where the size is greater than 1000 bytes.
Fill all three blanks to create a safe file reading dictionary comprehension filtering files with '.txt' extension.
file_contents = [1]: open([2], 'r').read() for [3] in filenames if [2].endswith('.txt')
The comprehension reads content from each file named 'filename' that ends with '.txt'.