Reading and Writing Files (I/O) Flashcards

This the basic methods for read and writing to files in Python

1
Q

fileObject.close() Method

A

Closes the file. Like Files->Save.. in your editor

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

fileObject.read() Method

A

Reads the contents of the file. You can assign the result to a variable

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

fileObject.readline() Method

A

Reads just one line of text file.

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

fileObject.truncate() Method

A

Empties the file. Watch out if you care about the file.

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

fileObject.write(stuff) Method

A

Writes stuff to the file.

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

open(filename, access_mode)

A

returns a file object in the specified mode(read, write), defaults to read, append, etc)

note: list of the methods can be found in pydoc file

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

file access mode ‘r’

A

Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

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

file access mode ‘rb’

A

Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

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

file access mode ‘r+’

A

Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

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

file access mode ‘rb+’

A

Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

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

file access mode ‘w’

A

Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

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

file access mode ‘wb’

A

Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

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

file access mode ‘w+’

A

Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

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

file access mode ‘wb+’

A

Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

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

file access mode ‘a’

A

Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

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

file access mode ‘ab’

A

Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

17
Q

file access mode ‘a+’

A

Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

18
Q

file access mode ‘ab+’

A

Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

19
Q

file.closed

A

Returns true if file is closed, false otherwise.

20
Q

file.mode

A

Returns access mode with which file was opened.

21
Q

file.name

A

Returns name of the file.

22
Q

file.softspace

A

Returns false if space explicitly required with print, true otherwise.