COSC 1306 - SET 4: Files
The statement f.write(10.0) always produces and error
True
When using a with statement to open a file, the file is automatically closed when the statements in the block finish executing.
True
Assign x to a bytes object containing three bytes with hexadecimal values 0x05, 0x15, and 0xf2. Use a bytes literal x = -- blank --
b'\x05\x15\xf2'
Assign x to a bytes object with a single byte whose hexadecimal value is 0x1a. use a bytes literal. x = -- blank --
b'\x1a'
Complete the program by echoing the second line of 'readme.txt' my_file = open('readme.txt') lines = my_files.readlines() print( -- blank -- )
lines[1]
Open 'myfile.txt as read-only in binary mode. f = --blank --
open('myfile.txt', 'rb')
Complete the statement to open the file "readme.txt" for reading. my_file = --blank--
open('readme.txt')
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')
The write() method immediately writes data to a file.
False
Use of a with statement is not recommended most of the time when opening files.
False
What does the call os.path.isfile('C:\\Program Files\\') return?
False
Fill in the arguments to os.path.join to assign file_path as 'subdir\\output.txt' (on Windows) file_path = os.path.join( -- blank -- )
'subdir', 'output.txt'
Data will be written to a new file. f = open('myfile.txt', --blank-- )
'w'
What does os.path.getsize(path_str) return?
The size in bytes of the file at path_str.
The flush() method (and perhaps os.fsync() as well) forces the output buffer to write to disk.
True
Assume that variable my_bytes is b'\x00\x04\xff\x00' Complete the statement to assign my_num to the 4-byte integer obtained by unpaking my_bytes. Use the byte ordering given by '>' my_num = struct.unpack( -- blank -- )
'>I', my_bytes
Complete the statement to pack an integer variable 'my_num' into a 2-byte sequence assigned to my_bytes. use the byte ordering given by '>' my_bytes = struct.pack( -- blank -- )
'>h', my_num
Data will be appended to the end of existing contents. f = open('myfile.txt', --blank-- )
'a'
Existing contents will be read, and new data will be appended. f = open('myfile.txt', --blank-- )
'a+'
Open 'data.txt' as read-only in binary mode f = open('data.txt', -- blank -- )
'rb'
What is returned by os.path.joinn('sounds', 'cars', 'honk.mp3') on Mac OS X? Use quotes in the answer
'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'
Complete the statement to create a csv module reader object to read mtfile.csv import csv with open('myfile.csv', 'r') as myfile: csv_reader = --blank--
csv.reader(myfile)
For a program run as 'python scriptname data.txt', what is sys.argv[1]? Do not use quotes in the answer
data.txt
Complete the statement to read up to 500 bytes from "readme.txt" into the contents variable my_file = open('readme.txt') contents = --blank--
my_file.read(500)
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
myscript.py infile.txt out
Complete the statement such 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( -- blank -- )
row[1]