External Files Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are some benefits of using external files?

A

Separation: cleaner and more modular code.
Reusability: Can be used across more than 1 file or program. Saves time and effort.
Memory management: Can be far more efficient (especially with larger data sets)
Collaboration: Makes it easier to work with other developers.

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

Try-except: What is else for?

A

Try should only contain code that may fail. Else contains what should happen normally.

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

Ways to open files.

A

open(filename, mode)
to close: filename.close()

with open(filename, mode) as name:

to close: done automatically

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

Modes of opening a file?

A

r - read
w- write
w+ or r+ - read and write
a - append
a+ - append and read
x - create*

t-text
b-binary

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

Difference between readline() and readlines()

A

.readline() reads a single line from a file (calling it more than once moves to different lines)
.readlines()
.read() reads the whole file

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

.strip()

A

removes whitespace (or as an argument, other characters)

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

using print() with files?

A

print(str, filename) writes the str to the file (this overwrites data)

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

writing to a file with .write()

A

.write(str) writes the str to the file. Remember newline characters.

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

making a new file?

A

open(new_file_name, w)
or
open(new_file_name,x)

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