Skip to content

File Handling in Python

Comprehensive Guide to File Handling in Python

Section titled “Comprehensive Guide to File Handling in Python”

File handling is a crucial aspect of programming, allowing you to read from and write to files. Python provides a set of functions and methods that make file handling straightforward. In this comprehensive guide, we will explore various aspects of file handling in Python, covering reading, writing, and manipulating files.

We can open a file in Python using the open() function. This function accepts two arguments, the name of the file and the mode in which we want to open the file. The mode can be read mode, write mode, append mode, etc. The default mode is read mode.

Syntax:

open.py
file_object = open(file_name, [access_mode], [buffering])

Here,

  • file_name is the name of the file that we want to access.
  • access_mode is the mode in which we want to open the file, i.e., read, write, append, etc.
  • buffering is an optional integer used to set the buffering policy.
  • file_object is the object that we can use to access the file.

There are different modes in which we can open a file. These modes are:

S.No.ModeDescription
1rOpens a file in read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.
2rbOpens a file in read-only mode in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
3r+Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
4rb+Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
5wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
6wbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
7w+Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
8wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
9aOpens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
10abOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
11a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
12ab+Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
13xOpens a file for exclusive creation. If the file already exists, the operation fails.
14xbOpens a file for exclusive creation in binary format. If the file already exists, the operation fails.
15x+Opens a file for exclusive creation. If the file already exists, the operation fails.
16xb+Opens a file for exclusive creation in binary format. If the file already exists, the operation fails.
17tOpens in text mode. (default)
18bOpens in binary mode.
19+Opens a file for updating (reading and writing)
20UUniversal newline mode.

When we are done with operations to be performed on the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file and is done using the close() method. Python has a garbage collector to clean up unreferenced objects but we must not rely on it to close the file.

Syntax:

close.py
file_object.close()

There are various methods available for this purpose. We can use the read(size) method to read in the size number of data. If the size parameter is not specified, it reads and returns up to the end of the file.

Syntax:

read.py
file_object.read([size])

Here,

  • file_object is the file object that we want to read.
  • size is an optional numeric argument. When it is provided, it reads that many characters from the file. If the size parameter is not specified, it reads and returns up to the end of the file.
  • The read() method returns the specified number of bytes from the file. If we omit the size argument, it returns and displays all the data from the file.

Example:

read.py
file_object = open("test.txt", "r")
data = file_object.read()
print(data)
file_object.close()

Output:

command
C:\Users\username>python read.py
Hello, World!
This is a test file.

Understanding file handling in Python is essential for a wide range of applications. Whether you’re reading data from a file, writing to it, or manipulating its content, Python’s file handling capabilities offer flexibility and ease of use. By following the principles outlined in this guide, you can efficiently work with files in Python, enabling you to build robust and scalable applications. Happy coding! For more tutorials, visit our Python Central Hub.


diagram two arguments decide whether a file survives the round trip mermaid
Opening a file involves more than a name and a mode. The encoding decides how bytes become text, and it defaults to whatever the platform happens to use rather than UTF-8. Getting it wrong does not usually raise -- it produces text that is quietly, subtly different from what was written.
sketch Reading a file with the wrong encoding does not raise p5.js
The same bytes, written as UTF-8 and read twice. Decoded as UTF-8 they come back unchanged; decoded with the platform default on this machine they come back as different characters, with no exception and no warning. That silence is the whole problem, because the corruption is only noticed much later.
pch.quizTag pch.quizDefaultTitle
  1. You write a file with `encoding='utf-8'` and read it back with plain `open(path)`. What happens?

    pch.quizShowAnswer

    C — It may decode into different characters, with no exception — Verified on this machine: the default was cp1252 and every non-ASCII character came back as different characters. Nothing raised — that silence is what makes the corruption travel.

  2. What does `locale.getpreferredencoding(False)` tell you?

    pch.quizShowAnswer

    B — The platform default used when you do not pass `encoding=` — It reported `cp1252` here; most Linux systems report `utf-8`. That is why the same script can produce different text on different machines.

  3. An exception is raised inside `with open(...) as f:`. Is the file closed?

    pch.quizShowAnswer

    B — Yes — that is what the context manager guarantees — Verified: `f.closed` was True after the exception. A manual `open()` with no `try`/`finally` left `closed` as False — with buffered data possibly never written.

  4. `sys.getdefaultencoding()` returns `'utf-8'`. Does that mean files default to UTF-8?

    pch.quizShowAnswer

    B — No — it governs `str`/`bytes` conversion, not file I/O — A genuinely misleading pair of names. File I/O uses `locale.getpreferredencoding(False)`, which reported `cp1252` here while `sys.getdefaultencoding()` said `utf-8`.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading