Files

Ace your homework & exams now with Quizwiz!

The flush() method (and perhaps os.fsync() as well) forces the output buffer to write to disk.

True

When using a with statement to open a file, the file is automatically closed when the statements in the block finish executing. True or False

True

Assign x with a bytes object containing three bytes with hexadecimal values 0x05, 0x15, and 0xf2. Use a bytes literal. x = ______________________________

b'\x05\x15\xf2'

Assign x with a bytes object with a single byte whose hexadecimal value is 0x1a. Use a bytes literal. x = ______________________________

b'\x1a'

Complete the program by echoing the second line of "readme.txt" my_file = open('readme.txt') lines = my_file.readlines() print(___________) # . . .

lines[1]

Open "myfile.txt" as read-only in binary mode. f = ______________________________

open('myfile.txt', 'rb')

Complete the statement to open the file "readme.txt" for reading. my_file = ___________________________________

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')

Use of a with statement is not recommended most of the time when opening files. True or False

False

What does the call *os.path.isfile('C:\\Program Files\\')* return?

False

The write() method immediately writes data to a file.

False

Open "data.txt" as read-only in binary mode. f = open('data.txt', _____)

'rb'

What does os.path.getsize(path_str) return?

The size in bytes of the file at path_str.

The statement *f.write(10.0)* always produces an error.

True

What is returned by os.path.join('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"

For each question, complete the statement to open myfile.txt with the appropriate mode. *Data will be written to a new file.* f = open('myfile.txt',_____)

'w'

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(_________________________________)

'>I', my_bytes

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(_________________________________)

'>h', my_num

For each question, complete the statement to open myfile.txt with the appropriate mode. *Data will be appended to the end of existing contents.* f = open('myfile.txt',_____)

'a'

For each question, complete the statement to open myfile.txt with the appropriate mode. *Existing contents will be read, and new data will be appended.* f = open('myfile.txt',_____)

'a+'

Fill in the arguments to os.path.join to assign file_path as "subdir\\output.txt" (on Windows). file_path = os.path.join (______________________________________________)

'subdir', 'output.txt'

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 = __________________________

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 = ___________________________________

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

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 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(___________________________)

row[1]


Related study sets

Perry- Chapter 48: Musculoskeletal or Articular Dysfunction

View Set

unit 8 The media phrasal verbs exercises

View Set

Operations Management Exam 2 Homework Questions

View Set

Episodic & Semantic Memory (Chapter 7)

View Set