Files Flashcards

(30 cards)

1
Q

What method is used to read the entire content of a file in Python?

A

The read() method.

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

True or False: The read() method can take an argument specifying the number of bytes to read.

A

True.

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

How do you open a file for reading in Python?

A

Use the open() function with ‘r’ mode.

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

What is the default mode for the open() function?

A

‘r’ (read mode).

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

Fill in the blank: To write to a file, you must open it in _____ mode.

A

‘w’ (write mode).

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

What method is used to read a CSV file in Python?

A

The csv.reader() method.

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

What library do you need to import to work with CSV files in Python?

A

The csv library.

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

True or False: The json library in Python is used to handle JSON data.

A

True.

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

What method is used to convert a Python object to a JSON string?

A

The json.dumps() method.

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

How do you write a JSON object to a file in Python?

A

Use json.dump() method.

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

What method is used to read a JSON file in Python?

A

The json.load() method.

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

What function is used to open a file in write mode and create it if it does not exist?

A

The open() function with ‘w’ mode.

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

Fill in the blank: The _____ method is used to write a row to a CSV file.

A

writerow()

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

What does the writeheader() method do in the csv module?

A

It writes the header row to the CSV file.

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

Which method would you use to read a specific number of lines from a file?

A

The readline() method.

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

True or False: The write() method can only append data to a file.

17
Q

What does the ‘a’ mode do when opening a file?

A

It opens the file for appending data.

18
Q

What method would you use to read all lines from a file into a list?

A

The readlines() method.

19
Q

How do you ensure that a file is properly closed after opening it?

A

Use a with statement.

20
Q

True or False: You can use the csv.DictReader() to read CSV files into a dictionary format.

21
Q

What is the purpose of the newline parameter in the open() function?

A

It controls how universal newlines work.

22
Q

Fill in the blank: The _____ method is used to write a string to a file.

23
Q

What Python function would you use to check if a file exists before opening it?

A

The os.path.exists() function.

24
Q

What is the difference between read() and readlines()?

A

read() returns the entire file as a string, while readlines() returns a list of lines.

25
How can you specify the delimiter when reading a CSV file?
By passing the delimiter argument to csv.reader().
26
True or False: The json.loads() method is used to parse a JSON string.
True.
27
What does the 'r+' mode do when opening a file?
It opens the file for both reading and writing.
28
What is the significance of the 'encoding' parameter when opening files?
It specifies the file's character encoding.
29
Fill in the blank: To read JSON data from a file, you would use _____ followed by load().
open()
30
What method can be used to write multiple rows to a CSV file at once?
The writerows() method.