Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
A. Here’s a hint: They are Sci-Fi related.

Workshop


Quiz


1. The os function is a built-in function. True or false?
2. Which file object methods can be used to read a text file in a random-access manner?
3. To open a file to be both read and written, which mode should be used in the open function
statement?
a. w+
b. r&w
c. r+

Answers


1. False. The os function is not a built-in function. You must import it to use its various methods.
To import the os function, you use the Python statement import os.
2. The .read and .seek file object methods can be used to read a text file in a random-access
(nonsequential) manner. You use the .seek method to put the file pointer in the correct
position in the file. You use the .read method to read a particular number of characters from
the file.
3. This is a trick question! Both answers a and c are correct. Remember that tacking on a + to
either r or w will allow the file to have the other option done to it as well. Therefore, w+ lets
you read and write to a file, and r+ also lets you read and write to a file.
Free download pdf