Zybooks Chapter 12 Files Participation Activity Questions
myscript.py infile.txt out
A script "myscript.py" has two command line arguments, one for an input file and a second for an output file. Type a command to run the program with input file "infile.txt" and output file "out". > python ____________________________________________
b'\x05\x15\xf2'
Assign x with a bytes object containing three bytes with hexadecimal values 0x05, 0x15, and 0xf2. Use a bytes literal. x = ____________________________________________
b'\x1a'
Assign x with a bytes object with a single byte whose hexadecimal value is 0x1a. Use a bytes literal. x = ____________________________________________
csv.reader(myfile)
The file "myfile.csv" contains the following contents: Airline,Destination,Departure time,Plane Southwest,Phoenix,615,B747 Alitalia,Milan,1545,B757 British Airways,London,1230,A380 Complete the statement to create a csv module reader object to read myfile.csv. import csv with open('myfile.csv', 'r') as myfile: csv_reader = ____________________________________________
True
The flush() method (and perhaps os.fsync()) forces the output buffer to write to disk. True False
True
The statement f.write(10.0) always produces an error . True False
False
The write() method immediately writes data to a file. True False
False
Use of a with statement is not recommended most of the time when opening files. True False
The size in bytes of the file at path_str.
What does os.path.getsize(path_str) return? The length of the path_str string. The combined size of all files in path_str directory. The size in bytes of the file at path_str.
False
What does the callos.path.isfile('C:\\Program Files\\')return? True False
"sounds\\cars\\honk.mp3"
What is returned by os.path.join('sounds', 'cars', 'honk.mp3') on Windows? Use quotes in the answer.
"sounds/cars/honk.mp3"
What is returned by os.path.join('sounds', 'cars', 'honk.mp3') on macOS? Use quotes in the answer.
('C:\\Programs\\Microsoft', 'msword.exe')
What is the output of the following program? import os p = 'C:\\Programs\\Microsoft\\msword.exe' print(os.path.split(p)) ['C:\\', 'Programs', 'Microsoft', 'msword.exe'] ('C:\\Programs\\Microsoft', 'msword.exe') ('C:', 'Programs', 'Microsoft', 'msword.exe')
True
When using a with statement to open a file, the file is automatically closed when the statements in the block finish executing. True False
'>I', my_bytes
Assume that variable my_bytes is b"\x00\x04\xff\x00". Complete the statement to assign my_num with the 4-byte integer obtained by unpacking my_bytes. Use the byte ordering given by ">". my_num = struct.unpack(____________________________________________)
lines[1]
Complete the program by echoing the second line of "readme.txt" my_file = open('readme.txt') lines = my_file.readlines() print(_____________________________________________ ) #....
row[1]
Complete the statement so that the program prints the destination of each flight in myfile.csv. import csv with open('myfile.csv', 'r') as myfile: csv_reader = csv.reader(myfile) for row in csv_reader: print(____________________________________________)
open('readme.txt')
Complete the statement to open the file "readme.txt" for reading. my_file = _____________________________________________
'>h', my_num
Complete the statement to pack an integer variable "my_num" into a 2-byte sequence. Assign my_bytes with the sequence. Use the byte ordering given by ">". my_bytes = struct.pack(____________________________________________)
my_file.read(500)
Complete the statement to read up to 500 bytes from "readme.txt" into the contents variable. my_file = open('readme.txt') contents = _____________________________________________ # ....
a
Data will be appended to the end of existing contents. f = open('myfile.txt', '_____________________________________________')
a+
Existing contents will be read, and new data will be appended. f = open('myfile.txt', '_____________________________________________')
'subdir', 'output.txt'
Fill in the arguments to os.path.join to assign file_path as "subdir\\output.txt" (on Windows). file_path = os.path.join(_____________________________________________)
data.txt
For a program run as "python scriptname data.txt", what is sys.argv[1]? Do not use quotes in the answer.
w
If the file exists, existing contents will be overwritten by new data. If not, data will be written to a new file. f = open('myfile.txt', '_____________________________________________')
'rb'
Open "data.txt" as read-only in binary mode. f = open('data.txt', ____________________________________________)
open('myfile.txt', 'rb')
Open "myfile.txt" as read-only in binary mode. f = ____________________________________________