Python: Files Flashcards

1
Q

What does the with command do in Python?

Opens a file in read-mode.

Creates a context-manager, which performs cleanup after exiting the adjacent indented block.

Imports a new module for use by the writer of the code.

A

Creates a context-manager, which performs cleanup after exiting the adjacent indented block.

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

Which of the following opens a file in Python?

with open(‘file1.txt’) as file_obj:
pass

with open(file.txt) as file_obj:
pass

with file_obj = open(‘file.txt’):
pass

A

with open(‘file1.txt’) as file_obj:
pass

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

What method reads a single line from a file object variable called file_object?

file_object.readlines()

file_object.readline()

file_object.read()

A

file_object.readline()

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

Which of the following methods on a file object (called file_object) reads the contents of a file and returns it as a string?

file_contents = file_object.read()

file_contents = file_object.readline()

file_contents = file_object.readlines()

file_contents = file_object.get()

A

file_contents = file_object.read()

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

What function would you use to render Python data to a JSON file?

json.dump()

json.writelines()

json.write()

A

json.dump()

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

Which of the following would you use to read in a CSV file as a dictionary?

csv.DictWriter

json.load

csv.DictReader

A

csv.DictReader

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

What different modes, passed as arguments to the open() function, are there for opening a file in Python?

Read-mode (‘r’, the default mode), Delete-mode (‘d’), and Update-mode (‘u’).

Read-mode (‘r’, the default mode), Write-mode (‘w’), and Update-mode (‘u’).

Read-mode (‘r’, the default mode), Write-mode (‘w’), and Append-mode (‘a’).

A

Read-mode (‘r’, the default mode), Write-mode (‘w’), and Append-mode (‘a’).

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