Complete the code to open a file named 'data.txt' in read mode.
file = open('data.txt', '[1]')
The mode 'r' opens the file for reading. Other modes are for writing or appending.
Complete the code to write the string 'Hello' to a file named 'output.txt'.
with open('output.txt', '[1]') as f: f.write('Hello')
The mode 'w' opens the file for writing and creates it if it doesn't exist.
Fix the error in the code to read all lines from 'log.txt' into a list.
with open('log.txt', 'r') as file: lines = file.[1]()
The method readlines() reads all lines into a list. readline() reads only one line.
Fill both blanks to create a dictionary with filenames as keys and their sizes in bytes as values.
import os files = ['a.txt', 'b.txt', 'c.txt'] sizes = { [1] : os.path.[2](f) for f in files }
Use f as the key for each filename, and os.path.getsize(f) to get the file size.
Fill all three blanks to filter files larger than 1000 bytes and create a dictionary with uppercase filenames as keys and sizes as values.
import os files = ['x.log', 'y.log', 'z.log'] large_files = { [1] : [2] for f in files if os.path.getsize(f) [3] 1000 }
Use f.upper() for uppercase keys, os.path.getsize(f) for values, and filter with > 1000 bytes.