Skip to content
QuiznExam

File I/O in Python

Test your knowledge of File I/O in Python, including opening files, reading and writing data, file modes, context managers, file methods, and handling text and binary files.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which built-in function is used to open a file in Python?

    • A. file()
    • B. open()
    • C. openFile()
    • D. readFile()
    Show answer

    B. open()

    The built-in open() function is used to open a file and return a file object.

  2. 2. Which file mode opens a file for reading?

    • A. w
    • B. a
    • C. r
    • D. x
    Show answer

    C. r

    The r mode opens a file for reading. It is the default mode when no mode is explicitly specified.

  3. 3. Which file mode opens a file for writing and truncates it if it already exists?

    • A. r
    • B. w
    • C. a
    • D. x
    Show answer

    B. w

    The w mode opens a file for writing and truncates the existing file if it already exists. If the file does not exist, it is created.

  4. 4. Which file mode opens a file for appending data to its end?

    • A. r
    • B. w
    • C. a
    • D. x
    Show answer

    C. a

    The a mode opens a file for appending. New data is written at the end of the file.

  5. 5. Which method reads the entire remaining contents of a text file?

    • A. read()
    • B. readline()
    • C. readall()
    • D. get()
    Show answer

    A. read()

    The read() method reads and returns the requested amount of data, or the remaining contents when no size is specified.

  6. 6. Which method reads a single line from a file?

    • A. readline()
    • B. readlineFile()
    • C. line()
    • D. readlineOnly()
    Show answer

    A. readline()

    readline() reads one line from the file, including the newline character when present.

  7. 7. Which method can be used to read all lines of a file into a list?

    • A. readlines()
    • B. allLines()
    • C. getlines()
    • D. readlist()
    Show answer

    A. readlines()

    readlines() reads the remaining lines and returns them as a list of strings.

  8. 8. Which method is used to write a string to a file?

    • A. write()
    • B. printFile()
    • C. writeText()
    • D. put()
    Show answer

    A. write()

    The write() method writes a string to the file and returns the number of characters written for text files.

  9. 9. What is the main purpose of using a `with` statement when working with files?

    • A. It automatically closes the file when the block is exited
    • B. It makes all files read-only
    • C. It prevents files from being created
    • D. It converts text files into binary files
    Show answer

    A. It automatically closes the file when the block is exited

    The with statement uses a context manager, ensuring that the file is properly closed when the block is exited, including in many error situations.

  10. 10. Which mode is used to work with a file as binary data?

    • A. t
    • B. b
    • C. binary
    • D. raw
    Show answer

    B. b

    The b mode indicates binary I/O. It is commonly combined with another mode, such as rb or wb.

  11. 11. What happens when `open('data.txt', 'x')` is used and the file already exists?

    • A. The existing file is truncated
    • B. Data is appended to the file
    • C. A FileExistsError is raised
    • D. The file is opened for reading
    Show answer

    C. A FileExistsError is raised

    The x mode is used for exclusive creation. It creates a new file and raises FileExistsError if the file already exists.

  12. 12. Which method changes the current position in a file?

    • A. seek()
    • B. move()
    • C. position()
    • D. goto()
    Show answer

    A. seek()

    The seek() method changes the file's current position to a specified location.

  13. 13. Which method returns the current position in a file?

    • A. position()
    • B. tell()
    • C. current()
    • D. where()
    Show answer

    B. tell()

    The tell() method returns the current file position as an integer.

  14. 14. Which statement correctly opens a file for reading and writing text?

    • A. open('data.txt', 'r')
    • B. open('data.txt', 'w')
    • C. open('data.txt', 'r+')
    • D. open('data.txt', 'a')
    Show answer

    C. open('data.txt', 'r+')

    The r+ mode opens an existing file for both reading and writing without truncating it.

  15. 15. What is a common result of calling `file.close()`?

    • A. The file object is closed and should no longer be used for I/O
    • B. The file is permanently deleted
    • C. The file contents are automatically erased
    • D. The file is reopened in read mode
    Show answer

    A. The file object is closed and should no longer be used for I/O

    close() closes the file object and releases associated resources. Attempting further I/O through the closed file generally raises an error.