Domain 3 - Recap - Working with files Flashcards
1
Q
When opening a file in python, how many arguments do you need to supply?
A
- 2 arguments
- One for the name and location of the file and the mode, such as read or write.
myFile = open(“text.txt”, “w”)
2
Q
When working with files, what do the following modes mean?
- w
- r
- r+
- a
- t
- x
A
- w = write
- r = write
- r+ = read and write
- a = append
- t = text
- x = open for exclusive creation, failing if the file already exists.
3
Q
What does f.read do in this example?
f = open(“textfile.txt”, “r”)
if f.mode = f.read()
contents = f.read()
print(contents)
f.close()
A
- f.read will take the entire contents of the file and print everything.
4
Q
What module is used to find paths to files in python?
A
import os
5
Q
A