#4 – File Handling Flashcards

(15 cards)

1
Q

How do you open a file for reading?

A

Use open() with mode 'r'.
~~~
with open(‘file.txt’, ‘r’) as f:
content = f.read()
~~~

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

How do you read a file line by line?

A

Use .readlines() or loop over the file object.
~~~
with open(‘file.txt’) as f:
for line in f:
print(line)
~~~

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

How do you write to a file (overwrite)?

A

Use mode 'w'.
~~~
with open(‘file.txt’, ‘w’) as f:
f.write(‘Hello’)
~~~

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

How do you append to a file?

A

Use mode 'a'.
~~~
with open(‘file.txt’, ‘a’) as f:
f.write(‘More text’)
~~~

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

How do you open a file in binary mode?

A

Add 'b' to the mode, e.g., 'rb', 'wb'.
~~~
with open(‘image.jpg’, ‘rb’) as f:
data = f.read()
~~~

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

What does with open(...) do?

A

It ensures the file is closed automatically after the block finishes, even if an error occurs.

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

How do you check if a file exists before opening it?

A

Use the os.path.exists() function.
~~~
import os if os.path.exists(‘file.txt’):
with open(‘file.txt’) as f:

~~~

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

How do you handle file-related errors safely?

A

Use a try-except block.
~~~
try:
with open(‘file.txt’) as f:
… except FileNotFoundError:
print(‘File not found’)
~~~

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

How do you read and write JSON data?

A

Use the json module.
~~~
import json with open(‘data.json’) as f:
data = json.load(f) with open(‘out.json’, ‘w’) as f:
json.dump(data, f)
~~~

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

How do you convert a Python object to a JSON string?

A

Use json.dumps().
~~~
json.dumps({‘a’: 1}) # returns: ‘{“a”: 1}’
~~~

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

How do you parse a JSON string into a Python object?

A

Use json.loads().
~~~
json.loads(‘{“a”: 1}’)
# returns: {‘a’: 1}
~~~

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

How do you move or rename a file?

A

Use os.rename().
~~~
import os os.rename(‘old.txt’, ‘new.txt’)
~~~

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

How do you delete a file?

A

Use os.remove().
~~~
import os os.remove(‘file.txt’)
~~~

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

How do you read only the first N bytes from a file?

A

Use .read(n).
~~~
with open(‘file.txt’) as f:
data = f.read(10)
~~~

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

How do you flush a file buffer manually?

A

Call f.flush().
~~~
f = open(‘file.txt’, ‘w’)
f.write(‘Hi’)
f.flush()
~~~

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