File Input / Output Flashcards

Outlines important commands for file input / output

1
Q

What does the function call open(filename, mode = ‘r’ do?

A

Creates a file reference

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

What are the requirement for the command mode = ‘r’ to work properly?

A

File must exist, only inspect what is there

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

What does the command mode = ‘w’ do?

A
  1. Builds up new contents for a file
  2. saved at close()
  3. Will replace any previously existing file with same name when opened
  4. Creates new file when needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the command mode = ‘a’ do?

A
  1. Same as writing but adds to existing contents (appends) rather than discarding existing contents
  2. Creates new file if needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which methods are allowed by mode = ‘r’ ?

A
  1. read
  2. readline
  3. readlines
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which methods are allowed by mode = ‘w’ or mode = ‘a’ ?

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

What does the close() function do?

A

Closes the file

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

What is a bookmark in this context?

A

Built in feature in Python that tracks where you are, updates as you go

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

What does the method call f.read(n) do?

A

Read up to n characters

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

What is the data type of the return value of f.read(n) ?

A

string

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

What is the purpose of the method call
f. read()?

A

read all remaining characters

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

What is the data type of the return value of f.read()?

A

string

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

What is the purpose of the method call f.readline(n)?

A

Read up to n characters on current line

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

What is the data type of the return value for f.readline(n)?

A

string

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

What is the purpose of the method call f.readline()

A

reads the rest of the current line

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

What is the data type of the return value of f.readline()?

17
Q

What is the purpose of the method call
f. readlines()?

A

read all the remaining lines

18
Q

What is the data type of the return value of f.readlines()?

A

A list of strings

19
Q

What does the method call write(x) do?

A

Writes string x to a file

20
Q

What does the method call writelines(xs) do?

A

Writes strings in list xs to a file

21
Q

What must be done before a file can be written to in Python?

A

It must be closed.

22
Q

What is calling the write() and writelines() function similar to?

A

Calling successive print calls, only the output goes to the file.

23
Q

What are two things that are never added when the write() and writelines() method is called? What is the only thing that is added?

A
  • No newlines or separators are ever added
  • Only exactly what you write is added
24
Q

What is reading files similar to?

A

Physically reading a book

25
Q

What are the contents of a file similar to?

A

A python strig, indexed 0 and up