Complete the code to open a large file for reading.
with open('large_file.txt', [1]) as file: pass
To read a file, you open it with mode 'r'.
Complete the code to read a large file line by line efficiently.
with open('large_file.txt', 'r') as file: for [1] in file: print(line.strip())
Each iteration reads one line, so 'line' is a good variable name.
Fix the error in the code to read a large file in chunks.
with open('large_file.txt', 'r') as file: while True: chunk = file.read([1]) if not chunk: break print(chunk)
The read() method expects an integer for the number of characters to read.
Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
We want the length of each word and only words longer than 3 characters.
Fill all three blanks to create a dictionary with uppercase keys and the original words as values for words longer than 4 characters.
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for word in words if len(word) [3] 4 }
Keys are uppercase words, values are original words, filtered by length > 4.
