Domain 3 - Recap - Working with files Flashcards

1
Q

When opening a file in python, how many arguments do you need to supply?

A
  1. 2 arguments
  2. One for the name and location of the file and the mode, such as read or write.

myFile = open(“text.txt”, “w”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When working with files, what do the following modes mean?

  1. w
  2. r
  3. r+
  4. a
  5. t
  6. x
A
  1. w = write
  2. r = write
  3. r+ = read and write
  4. a = append
  5. t = text
  6. x = open for exclusive creation, failing if the file already exists.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
  1. f.read will take the entire contents of the file and print everything.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What module is used to find paths to files in python?

A

import os

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly