πŸŽ“ Velnov

🐍 Python · Topic 12 of 20

File Handling β€” Python Questions with Answers

Start with the study notes on File Handling (definitions, examples and a quick revision), then work through 30 practice questions, each with the correct answer and a clear explanation of why it is right. When you are ready, take this topic as an interactive quiz to test yourself.

10 easy Β· 14 medium Β· 6 hard Β· Part of Python: Master Python from basics to advanced - 20 topics.

Topic overview

File Handling – reading and writing files

1. Opening a file

open(path, mode) returns a file object. The mode says what you want to do:

Remember: Opening an existing file with "w" wipes it immediately, even if you write nothing. Use "a" to add to it.

2. Use with to close files automatically

The with statement closes the file when the block ends, even if an error happens. It is the recommended way to work with files.

with open("notes.txt", "w", encoding="utf-8") as f:
    f.write("first line\n")
    f.write("second line\n")

print(f.closed)
OutputTrue

3. Writing

total = 42
with open("out.txt", "w", encoding="utf-8") as f:
    f.write(f"Total: {total}\n")
    print("Done", file=f)
with open("out.txt", encoding="utf-8") as f:
    print(f.read(), end="")
OutputTotal: 42
Done

4. Reading

with open("notes.txt", "w", encoding="utf-8") as f:
    f.write("cat\ndog\nbird\n")

with open("notes.txt", encoding="utf-8") as f:
    for line in f:
        print(line.strip())
Outputcat
dog
bird

Remember: Each line ends with an invisible \n. Use line.strip() before comparing or printing it.

5. The file position

A file object remembers where it is. After read(), the position is at the end, so a second read() returns an empty string. seek(0) goes back to the start.

with open("notes.txt", "w", encoding="utf-8") as f:
    f.write("hello")
with open("notes.txt", encoding="utf-8") as f:
    print(repr(f.read()))
    print(repr(f.read()))
    f.seek(0)
    print(repr(f.read()))
Output'hello'
''
'hello'

6. Handling missing files

import os
try:
    with open("ghost.txt", encoding="utf-8") as f:
        data = f.read()
except FileNotFoundError:
    data = ""
print(repr(data), os.path.exists("ghost.txt"))
Output'' False

Example: Simple CSV: name, age, city = line.strip().split(","). For real CSV files with quoted commas, use the csv module.

7. Saving data with JSON

The json module saves lists and dicts as text and loads them back. dump/load work with files; dumps/loads work with strings.

import json
scores = {"Ana": 92, "Ben": 85}
with open("save.json", "w", encoding="utf-8") as f:
    json.dump(scores, f)
with open("save.json", encoding="utf-8") as f:
    loaded = json.load(f)
print(loaded["Ana"])
print(json.dumps([1, 2, 3]))
Output92
[1, 2, 3]

8. Good habits

9. Common misconceptions

MisconceptionCorrect understanding
"w" mode adds to a file."w" erases the file first; "a" adds to the end.
write() adds a new line like print().write() writes only what you pass; add \n yourself.
You can call f.read() after the with block.The file is closed there; reading raises ValueError.
A second read() reads the file again.The position is at the end; use seek(0) first.
write(42) works.write() needs a string: write(str(42)).

10. Quick revision

"r"
Read mode; the file must exist.
"w"
Write mode; erases the file first.
"a"
Append mode; adds to the end.
with
Closes the file automatically.
read()
Whole file as one string.
for line in f
Reads one line at a time.
seek(0)
Back to the start of the file.
json.dump / json.load
Save / load data as JSON.

11. Test your understanding

  1. Which mode adds to the end of a file?

    Show answer

    "a" (append).

  2. What is the danger of "w" mode?

    Show answer

    It erases the existing content as soon as the file opens.

  3. Why use with open(...) as f:?

    Show answer

    The file is closed automatically, even if an error happens.

  4. What is the best way to read a huge file?

    Show answer

    for line in f: one line at a time.

  5. Why call line.strip() on lines from a file?

    Show answer

    Each line ends with a \n character.

  6. What does a second f.read() return?

    Show answer

    An empty string, because the position is at the end.

  7. Which error comes from opening a missing file for reading?

    Show answer

    FileNotFoundError.

  8. How do you save a dict to a file?

    Show answer

    json.dump(data, f)

Key takeaway: Open files with with, choose the right mode (beware "w"), remember write() adds no new line, and use json to save structured data.

Notes written by Velnov. Every code example with an output was run in Python 3 to check that the output shown is exactly what Python prints.

Key concepts in this topic

open()File modesFileNotFoundErrorWrite modeAppend modeLogswith statementContext managersread()readlines()readline()IterationMemory efficiencystrip()Newlineswrite()TypeErrorstr()try/exceptExclusive modeFileExistsErrorBinary modebytesValueError

Jump to: Easy Β· Medium Β· Hard

Easy questions (10)

Q1. easy

Which function opens a file in Python?

f = open("notes.txt", "r")
text = f.read()
f.close()
  1. A open()
  2. B file()
  3. C read()
  4. D load()

Answer: A β€” open()

open(path, mode) returns a file object: open("notes.txt", "r") opens for reading. The mode defaults to "r" if omitted.

open()File modes

Q2. easy

What does mode "r" mean in open("data.txt", "r")?

  1. A Read - file must already exist
  2. B Remove the file
  3. C Rename the file
  4. D Read and write

Answer: A β€” Read - file must already exist

"r" opens for READING only. If the file doesn't exist you get FileNotFoundError. Other modes: "w" write, "a" append, "x" create-new.

File modesFileNotFoundError

Q4. easy

Which mode ADDS to the end of a file without deleting what's there?

with open("log.txt", "a") as f:
    f.write("user logged in\n")
  1. A "w"
  2. B "a"
  3. C "r"
  4. D "end"

Answer: B β€” "a"

"a" = append. Writes always go to the end; existing content is safe. Perfect for log files. The file is created if it doesn't exist.

Append modeLogs

Q5. easy

What is the PREFERRED way to open files, and why?

with open("notes.txt") as f:
    text = f.read()
# file is ALREADY closed here
  1. A with open(...) as f: - the file closes automatically, even on errors
  2. B f = open(...) - simpler
  3. C It makes no difference
  4. D global open

Answer: A β€” with open(...) as f: - the file closes automatically, even on errors

The with statement (context manager) guarantees the file is closed when the block ends - even if an exception occurs inside. No forgotten close(), no leaked file handles.

  • Modern Python code almost never calls close() manually.

with statementContext managers

Q6. easy

What does f.read() return?

with open("poem.txt") as f:
    text = f.read()      # one big string
print(len(text))
  1. A The ENTIRE file content as one string
  2. B One line
  3. C A list of lines
  4. D The file size

Answer: A β€” The ENTIRE file content as one string

read() slurps the whole file into a single string (newlines included). readline() gives one line; readlines() gives a list of all lines.

read()readlines()

Q10. easy

How do you WRITE text to an open file f?

with open("out.txt", "w") as f:
    f.write("hello\n")
    f.write("world\n")
  1. A f.write("hello")
  2. B f.print("hello")
  3. C write(f, "hello")
  4. D f << "hello"

Answer: A β€” f.write("hello")

f.write(string) writes exactly the string you give - and returns the number of characters written.

write()

Q13. easy

Opening a missing file in "r" mode raises:

try:
    with open("ghost.txt") as f:
        data = f.read()
except FileNotFoundError:
    data = ""
  1. A FileNotFoundError
  2. B ValueError
  3. C KeyError
  4. D Nothing - it creates the file

Answer: A β€” FileNotFoundError

Read mode requires the file to exist. Write ("w") and append ("a") modes CREATE missing files instead.

FileNotFoundErrortry/except

Q24. easy

How do you count the lines in a file?

with open("data.txt") as f:
    count = sum(1 for _ in f)
print(count, "lines")
  1. A sum(1 for line in f) or len(f.readlines())
  2. B f.count_lines()
  3. C f.length
  4. D count(f)

Answer: A β€” sum(1 for line in f) or len(f.readlines())

Iterate and count: sum(1 for line in f) is memory-friendly; len(f.readlines()) is fine for small files. There's no built-in line counter.

IterationGenerator expressions

Q26. easy

Which snippet appends one log entry safely?

  1. A with open("app.log", "a") as f:\n f.write("login ok\n")
  2. B with open("app.log", "w") as f:\n f.write("login ok\n")
  3. C open("app.log").write("login ok")
  4. D append("app.log", "login ok")

Answer: A β€” with open("app.log", "a") as f:\n f.write("login ok\n")

Mode "a" preserves old entries; with guarantees the file closes. Option B wipes the log each time; option C never closes and uses read mode.

Append modewith statement

Q29. easy

After 'with open(...) as f:' ends, is the file open or closed?

with open("a.txt") as f:
    pass
print(f.closed)   # True
  1. A Closed - automatically, always
  2. B Open until program exit
  3. C Open until garbage collection
  4. D Depends on the mode

Answer: A β€” Closed - automatically, always

That's the whole point of with: the file is closed the instant the block exits - normally or via an exception. f.closed is True afterwards.

Context managers

Medium questions (14)

Q3. medium

What is the DANGER of mode "w"?

open("log.txt", "w")   # content GONE
open("log.txt", "a")   # content kept, writes go to the end
  1. A It's slower
  2. B It ERASES the file's existing content the moment it opens
  3. C It can't create new files
  4. D It locks the file forever

Answer: B β€” It ERASES the file's existing content the moment it opens

"w" truncates: opening an existing file in "w" instantly wipes it - even if you never write anything. Use "a" (append) to keep existing content and add to the end.

  • Real data has been lost to this. Double-check your mode!

Write modeAppend mode

Q7. medium

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

with open("data.csv") as f:
    header = f.readline()     # first line only
    rest = f.readlines()       # remaining lines as a list
  1. A readline() returns the NEXT single line; readlines() returns ALL lines as a list
  2. B They're identical
  3. C readlines() reads one line
  4. D readline() returns a list

Answer: A β€” readline() returns the NEXT single line; readlines() returns ALL lines as a list

readline() advances one line per call - good for reading just the header. readlines() loads every line into a list at once - convenient, but memory-heavy for huge files.

readline()readlines()

Q8. medium

What is the BEST way to process a large file line by line?

with open("big.log") as f:
    for line in f:
        if "ERROR" in line:
            print(line.strip())
  1. A for line in f: - iterate the file object directly
  2. B f.read().split() always
  3. C readlines() then loop
  4. D while f.next():

Answer: A β€” for line in f: - iterate the file object directly

A file object is iterable - looping it reads ONE line at a time, so even gigabyte files use tiny memory. readlines() would load everything at once.

IterationMemory efficiency

Q9. medium

Why call line.strip() when reading lines from a file?

for line in f:
    name = line.strip()
    if name == "cat":    # now matches
        print("found")
  1. A Each line ends with an invisible \n newline - strip() removes it
  2. B To remove all spaces inside the line
  3. C Files require it
  4. D To convert to lowercase

Answer: A β€” Each line ends with an invisible \n newline - strip() removes it

Lines keep their trailing newline character, so print(line) double-spaces and comparisons fail ('cat\n' != 'cat'). strip() (or rstrip()) cleans it off.

strip()Newlines

Q11. medium

What's the catch with f.write() compared to print()?

f.write("a")
f.write("b")     # file contains: ab
f.write("c\n")   # add \n yourself
print("d", file=f)  # or use print
  1. A write() does NOT add a newline automatically - you must include \n yourself
  2. B write() adds two newlines
  3. C write() can't write strings
  4. D No difference

Answer: A β€” write() does NOT add a newline automatically - you must include \n yourself

print() appends \n for you; write() writes ONLY what you pass. Forgetting \n glues all your lines into one. Alternative: print("text", file=f) adds the newline.

write()Newlines

Q12. medium

What happens with f.write(42)?

total = 42
f.write(str(total) + "\n")
f.write(f"Total: {total}\n")   # cleaner
  1. A Writes 42
  2. B TypeError - write() needs a string, so convert: f.write(str(42))
  3. C Writes '42' automatically
  4. D Writes binary

Answer: B β€” TypeError - write() needs a string, so convert: f.write(str(42))

write() only accepts strings (in text mode). Numbers must be converted first: str(42) or an f-string f"{42}\n".

TypeErrorstr()

Q15. medium

When do you need mode "rb" or "wb"?

with open("photo.jpg", "rb") as src:
    data = src.read()          # bytes
with open("copy.jpg", "wb") as dst:
    dst.write(data)
  1. A For BINARY files - images, audio, zip files - where data is bytes, not text
  2. B For very big text files
  3. C For read-backwards mode
  4. D Never - text mode handles everything

Answer: A β€” For BINARY files - images, audio, zip files - where data is bytes, not text

The b suffix switches to binary mode: read() returns bytes instead of str, and no newline translation happens. Copying an image in text mode corrupts it.

Binary modebytes

Q16. medium

What happens when you read from a file AFTER closing it?

with open("a.txt") as f:
    data = f.read()      # inside: OK
# f.read()               # outside: ValueError!
  1. A Returns empty string
  2. B ValueError: I/O operation on closed file
  3. C Reopens automatically
  4. D Returns None

Answer: B β€” ValueError: I/O operation on closed file

A closed file object refuses all operations with ValueError. A common bug: doing f.read() OUTSIDE the with block - the file is already closed there.

with statementValueError

Q19. medium

A simple way to parse the CSV line "Ana,21,Delhi"?

with open("students.csv") as f:
    for line in f:
        name, age, city = line.strip().split(",")
        print(name, age)
  1. A line.strip().split(",") -> ['Ana', '21', 'Delhi']
  2. B line.parse()
  3. C line.csv()
  4. D int(line)

Answer: A β€” line.strip().split(",") -> ['Ana', '21', 'Delhi']

split(",") cuts a comma-separated line into fields. For real CSV files with quoted commas, use the csv module - but split() handles simple data fine.

split()csv module

Q20. medium

Which module converts Python dicts to/from the JSON text format?

import json

data = {"name": "Ana", "score": 92}
with open("save.json", "w") as f:
    json.dump(data, f)

with open("save.json") as f:
    loaded = json.load(f)
print(loaded["score"])   # 92
  1. A json
  2. B dict
  3. C text
  4. D xml

Answer: A β€” json

The built-in json module: json.dump(data, f) writes a dict as JSON to a file; json.load(f) reads it back. The universal format for configs and APIs.

json moduleSerialization

Q22. medium

How do you check whether a file exists BEFORE opening it?

import os
if os.path.exists("save.json"):
    load_game()
else:
    new_game()
  1. A os.path.exists("data.txt") (or pathlib's Path("data.txt").exists())
  2. B open.exists("data.txt")
  3. C file.check("data.txt")
  4. D exists "data.txt"

Answer: A β€” os.path.exists("data.txt") (or pathlib's Path("data.txt").exists())

os.path.exists() returns True/False. (The EAFP alternative: just open it and catch FileNotFoundError - equally Pythonic.)

os.pathpathlibEAFP

Q23. medium

Why pass encoding="utf-8" to open()?

with open("notes.txt", encoding="utf-8") as f:
    text = f.read()   # handles any language
  1. A So text with any language/emoji reads and writes correctly on every OS
  2. B It compresses the file
  3. C It's required for numbers
  4. D It makes files read-only

Answer: A β€” So text with any language/emoji reads and writes correctly on every OS

Without it, Python uses a platform-dependent default (often cp1252 on Windows) - files with Γ±, δΈ­ζ–‡ or emoji can crash or garble. Explicit utf-8 makes your code portable.

  • Good habit: always specify encoding for text files.

EncodingUnicode

Q25. medium

What does writelines() do - and what does it NOT do?

lines = ["one\n", "two\n", "three\n"]
with open("out.txt", "w") as f:
    f.writelines(lines)
  1. A Writes each string from a list; it does NOT add newlines between them
  2. B Writes lines and adds newlines automatically
  3. C Reads lines
  4. D Writes one line only

Answer: A β€” Writes each string from a list; it does NOT add newlines between them

Despite the name, writelines(list) just concatenates the strings - no separators added. Include \n in each string yourself.

writelines()Newlines

Q28. medium

What does mode "r+" allow?

  1. A Both reading AND writing, without erasing the file first
  2. B Reading twice as fast
  3. C Reading with automatic retry
  4. D Read-only, but raises no errors

Answer: A β€” Both reading AND writing, without erasing the file first

"r+" opens an EXISTING file for read and write - the cursor starts at the beginning, and writes overwrite bytes in place. ("w+" also reads+writes but truncates first.)

File modesr+ vs w+

Hard questions (6)

Q14. hard

What does mode "x" do?

try:
    with open("config.ini", "x") as f:
        f.write("[defaults]\n")
except FileExistsError:
    print("config already exists - not touching it")
  1. A Creates a NEW file - fails with FileExistsError if it already exists
  2. B Extracts the file
  3. C Executes the file
  4. D Deletes the file

Answer: A β€” Creates a NEW file - fails with FileExistsError if it already exists

"x" is exclusive creation - a safe 'create only if new'. Unlike "w" it can never accidentally wipe an existing file.

Exclusive modeFileExistsError

Q17. hard

Why does the SECOND read() return an empty string? data1 = f.read() data2 = f.read()

data1 = f.read()   # everything; cursor at end
data2 = f.read()   # '' - nothing left
f.seek(0)           # rewind
data3 = f.read()   # everything again
  1. A The file position is at the END after the first read - there's nothing left to read
  2. B Files can only be read once ever
  3. C It's a bug in Python
  4. D The file gets deleted after reading

Answer: A β€” The file position is at the END after the first read - there's nothing left to read

A file object keeps a position cursor. read() moves it to the end; the next read() starts there and finds nothing. f.seek(0) rewinds to the beginning to read again.

File positionseek()

Q18. hard

What does f.seek(0) do?

  1. A Moves the read/write position back to the START of the file
  2. B Deletes the first byte
  3. C Searches for the number 0
  4. D Closes the file

Answer: A β€” Moves the read/write position back to the START of the file

seek(position) jumps the file cursor to that byte offset - seek(0) rewinds to the beginning. f.tell() reports the current position.

seek()tell()

Q21. hard

What is the difference between json.dump() and json.dumps()?

import json
text = json.dumps({"a": 1})   # '{"a": 1}' - a string
data = json.loads(text)        # back to a dict
  1. A dump() writes to a FILE; dumps() returns a STRING (s = string)
  2. B dumps() is the plural version for lists
  3. C dump() deletes the data after
  4. D No difference

Answer: A β€” dump() writes to a FILE; dumps() returns a STRING (s = string)

The trailing s means 'string': dumps(data) gives you the JSON text to use however you like; dump(data, f) writes it straight into a file. Same pair for reading: loads(text) vs load(f).

json moduledumps vs dump

Q27. hard

Copy a text file. Fill the blank: with open("a.txt") as src, ______ as dst: dst.write(src.read())

with open("a.txt") as src, open("b.txt", "w") as dst:
    dst.write(src.read())
  1. A open("b.txt", "w")
  2. B open("b.txt", "r")
  3. C copy("b.txt")
  4. D open("b.txt")

Answer: A β€” open("b.txt", "w")

One with statement can manage MULTIPLE files (comma-separated). The destination needs write mode; options B/D open read-only and writing would fail.

Multiple context managersCopying

Q30. hard

You need to save a Python LIST so another run of your program can reload it. Best simple approach?

import json
scores = [90, 85, 77]
with open("scores.json", "w") as f:
    json.dump(scores, f)

with open("scores.json") as f:
    scores = json.load(f)
  1. A json.dump() it to a file, json.load() to restore
  2. B write(str(list)) and eval() it back
  3. C Print it and copy-paste later
  4. D Lists can't be saved

Answer: A β€” json.dump() it to a file, json.load() to restore

JSON handles lists, dicts, strings, numbers and bools - safe and readable. eval() on file content is a security hole (it executes anything). For arbitrary Python objects, the pickle module exists - but JSON is the safe default.

jsonpickleeval() dangers

Practice β€œFile Handling” as a quiz