Programming Concepts 004 Flashcards

1
Q

What is “with open” used for?

A

It is used for explicit opening and closing of text files.

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

What will this do?

with open(“test.txt”. ‘r’) as f:
file_content = f.read()
print(file_content)

A

It will open then print all the contents of test.txt.

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

What will happen?

with open(“test.txt”, ‘r’) as f:
file_content = f.read()
print(file_content)

print(f.read())

A

It will error as it is trying to access a closed file when it is printing.

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

What does read() do?

A

It reads a file as on single string.

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

What does readlines() do?

A

It reads every separate line and stores them in a list based on the next line character.

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

What does r and r+ do?

A

They both attempt to read a file but the + allows you to write to the file as well.

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

What does w and w+ do?

A

They both attempt to write to a file and also can make a new file but the + also allows for reading from a file.

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

What does a and a+ do?

A

They both act like w and w + where they write to a file but instead of overwriting everything it just adds to the end of the file.

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

What purpose do we write to and read files?

A

It can be used for getting data. This is like saving in a game for you stats and progression. It just means it can be continued after the program turns off.

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