Files Questions with Answers

7 practice questions on Files (Python) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

Which mode passed to open() adds new data to the end of an existing file without erasing it?

  1. "w"
  2. "r"
  3. "x"
  4. "a"

Answer: D. "a"

Explanation

'a' (append) positions writes at the end of the file and creates the file if it does not exist.

Step-by-step solution

  1. 'a' means append: writes go to the end of the file.
  2. The old text is kept, and the file is created if it does not exist.
  3. So 'a' is the answer, the correct answer.

Why the other options are wrong

  • A. 'w' erases the file first, and then writes.
  • B. 'r' opens the file for reading only, so it cannot add data.
  • C. 'x' creates a new file only and fails if the file already exists.

Tip: 'w' overwrites, 'a' appends, 'x' creates only a new file, and 'r' reads.

Question 2Easy

What is the main benefit of using with open(path) as f: ?

  1. It closes the file automatically, even if an error occurs
  2. It reads the file faster by buffering it all in memory
  3. It locks the file so that other processes cannot open it
  4. It always opens the file in binary rather than text mode

Answer: A. It closes the file automatically, even if an error occurs

Explanation

The with statement uses the file's context manager, which calls close() when the block exits, normally or because of an exception.

Step-by-step solution

  1. The with block uses the file as a context manager.
  2. When the block ends, the file is closed, even if an exception happens inside it.
  3. So the main benefit is automatic closing, the correct answer.

Why the other options are wrong

  • B. With does not change how fast the file is read.
  • C. With does not lock the file against other programs.
  • D. With does not force binary mode; text mode is the default.

Tip: Use with for files so you never forget to close them.

Question 3Medium

A file notes.txt already contains text. What happens after this code runs?

with open("notes.txt", "w") as f:
    f.write("new")
  1. The old text is kept and nothing is written
  2. The file contains only "new"
  3. A FileExistsError is raised
  4. "new" is appended to the old text

Answer: B. The file contains only "new"

Explanation

'w' mode truncates the file to zero length when opening it, so the old contents are lost.

Step-by-step solution

  1. The 'w' mode opens the file and cuts it to length zero.
  2. The old text is erased as soon as the file is opened.
  3. Then 'new' is written, so the file contains only new, the correct answer.

Why the other options are wrong

  • A. 'w' does not keep the old text; it erases it.
  • C. FileExistsError comes from 'x' mode, not from 'w'.
  • D. 'a' appends, but 'w' replaces the content.

Tip: 'w' erases the file and 'a' appends to it.

Question 4Medium

What is the output?

with open("t.txt", "w") as f:
    print(f.write("hello"))
  1. 5
  2. hello
  3. True
  4. None

Answer: A. 5

Explanation

For a text file, write() returns the number of characters written.

Step-by-step solution

  1. f.write('hello') writes 5 characters and returns 5.
  2. Print shows the returned value.
  3. So the output is 5, the correct answer.

Why the other options are wrong

  • B. Hello is the text that was written, but write returns the count.
  • C. True is wrong; write returns a number, not a boolean.
  • D. None is wrong; write returns the number of characters written.

Tip: For text files, write() returns the number of characters written.

Question 5Medium

What is the output?

with open("t.txt", "w") as f:
    f.write("ab\ncd\n")

with open("t.txt") as f:
    print(f.readlines())
  1. ['ab\ncd\n']
  2. ['ab', 'cd']
  3. ['ab\n', 'cd\n']
  4. ['ab\n', 'cd\n', '']

Answer: C. ['ab\n', 'cd\n']

Explanation

readlines() returns a list of lines and keeps the trailing newline on each one.

Step-by-step solution

  1. The file holds the text ab, a newline, cd and a newline.
  2. readlines() returns one string per line, and each string keeps its newline at the end.
  3. So the output is ['ab\n', 'cd\n'], the correct answer.

Why the other options are wrong

  • A. The text has two lines, so the list has two items, not one.
  • B. The newline characters stay at the end of each item, so the items are not bare ab and cd.
  • D. The file ends with a newline, so there is no extra empty line.

Tip: readlines() keeps the newline at the end of each line. strip() removes it.

Question 6Hard

What does open("report.txt", "x") do if report.txt already exists?

  1. Raises FileExistsError
  2. Appends to it
  3. Truncates it
  4. Opens it for reading and writing

Answer: A. Raises FileExistsError

Explanation

'x' is exclusive creation: it creates a new file and fails with FileExistsError if the file already exists.

Step-by-step solution

  1. 'x' means exclusive creation: it makes a new file only.
  2. If the file already exists, Python raises FileExistsError.
  3. So the answer is that it raises FileExistsError, the correct answer.

Why the other options are wrong

  • B. Appending is the 'a' mode, not 'x'.
  • C. Truncating is the 'w' mode, not 'x'.
  • D. 'x' does not open an existing file; it fails instead.

Tip: 'x' is safe for creating new files, because it never overwrites an existing one.

Question 7Hard

What is the output?

from pathlib import Path
p = Path("data/archive.tar.gz")
print(p.suffix, p.stem)
  1. .tar.gz archive
  2. .gz archive
  3. gz archive.tar
  4. .gz archive.tar

Answer: D. .gz archive.tar

Explanation

suffix is only the last extension ('.gz'), and stem is the final name without that last suffix ('archive.tar'). Use p.suffixes for ['.tar', '.gz'].

Step-by-step solution

  1. Suffix is the last extension only, which is .gz.
  2. Stem is the name without the last suffix, which is archive.tar.
  3. So the output is .gz archive.tar, the correct answer.

Why the other options are wrong

  • A. Suffix gives only the last extension, .gz, not .tar.gz.
  • B. Stem keeps the .tar part, so it is archive.tar, not archive.
  • C. Suffix includes the dot, so it is .gz, not gz.

Tip: Path.suffixes gives every extension as a list, such as ['.tar', '.gz'].

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics