Python – File I/O

This tutorial will teach you how to work with files in Python. More specifically, how to open a file, read from it, write into it, and close it, as well as numerous file operations to be aware of.


Files are identified locations on disk where associated data is stored. They’re used to keep data in a non-volatile memory for a long time (e.g. hard disk).

We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).

We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.

Hence, in Python, a file operation takes place in the following order:

  1. Open a file
  2. Read or write (perform operation)
  3. Close the file

Opening Files in Python

To open a file in Python, use the open() function. This function returns a file object, often known as a handle, which can be used to read or change the file.

>>> f = open("test.txt")    # open file in current directory
>>> f = open("C:/Python38/README.txt")  # specifying full path

When we open a file, we can define the mode. We specify whether we want to read r, write w, or append a to the file with the mode parameter. We can additionally specify whether the file should be opened in text or binary form.

Reading in text mode is the default setting. When reading from the file in this manner, we get strings.

Binary mode, on the other hand, returns bytes and is the mode to use when working with non-text files such as images or executable files.

rOpens a file for reading. (default)
wOpens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
xOpens a file for exclusive creation. If the file already exists, the operation fails.
aOpens a file and appends to the end of it without truncating it. If the file does not exist, it is created.
tOpens in text mode. (default)
bOpens in binary mode.
+Opens a file for updating (reading and writing)
f = open("test.txt")      # equivalent to 'r' or 'rt'
f = open("test.txt",'w')  # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode

The character a, unlike in other languages, does not suggest the number 97 until it is encoded using ASCII (or other equivalent encodings).

Furthermore, the default encoding varies per platform. In Windows, it’s cp1252, whereas in Linux, it’s utf-8.

As a result, we should not rely on the default encoding because our code will react differently across platforms.

As a result, specifying the encoding type while working with files in text mode is highly advised.

f = open("test.txt", mode='r', encoding='utf-8')

Closing Files in Python

We must correctly close the file once we have finished performing actions on it.

When you close a file, the resources associated with it are released. The close() function in Python is used to do this.

The garbage collector in Python cleans away unreferenced objects, but we shouldn’t rely on it to shut the file.

f = open("test.txt", encoding = 'utf-8')
# perform file operations

This procedure isn’t completely risk-free. The code departs without closing the file if an exception occurs while performing some operation with it.

Using a try…finally block is a safer option.

   f = open("test.txt", encoding = 'utf-8')
   # perform file operations

We can ensure that the file is properly closed even if an exception is generated, causing program flow to halt.

The with statement is the most effective technique to close a file. When the block inside the with statement is exited, the file will be closed.

The close() method does not need to be called manually. Internally, it is carried out.

with open("test.txt", encoding = 'utf-8') as f:
   # perform file operations

Writing to Files in Python

In Python, we must open the file in write w, append a, or exclusive creation x mode in order to write into it.

We must be cautious while using the w mode because it will overwrite a file that already exists. As a result, all previous data is deleted.

The write() method is used to write a string or a sequence of bytes (for binary files). The number of characters written to the file is returned by this method.

with open("test.txt",'w',encoding = 'utf-8') as f:
   f.write("my first file\n")
   f.write("This file\n\n")
   f.write("contains three lines\n")

If test.txt does not already exist in the current directory, this application will create it. If it exists, it has been overwritten.

To separate the different lines, we must include the newline characters ourself.

Reading Files in Python

We must open a file in reading r mode in Python to read it.

This can be accomplished using a variety of approaches. To read in the size number of data, we can use the read(size) method. It reads and returns up to the end of the file if the size option is not given.

The text.txt file we created in the previous step can be read in the following way:

>>> f = open("test.txt",'r',encoding = 'utf-8')
>>>    # read the first 4 data

>>>    # read the next 4 data
' is '

>>>     # read in the rest till end of file
'my first file\nThis file\ncontains three lines\n'

>>>  # further reading returns empty sting

The read() method, as we can see, returns a newline as ‘n’. On continued reading, we obtain an empty string after we reach the conclusion of the file.

The seek() method can be used to modify the current file cursor (position). The inform() method, on the other hand, returns our present location (in number of bytes).

>>> f.tell()    # get the current file position

>>>   # bring file cursor to initial position

>>> print(  # read the entire file
This is my first file
This file
contains three lines

We can read a file line-by-line using a for loop. This is both efficient and fast.

>>> for line in f:
...     print(line, end = '')
This is my first file
This file
contains three lines

The lines in the file itself contain the newline character n in this software. To prevent writing two newlines, we use the end option of the print() function.

We can also read individual lines from a file using the readline() method. This method reads a file until it reaches the newline character, which includes the newline character.

>>> f.readline()
'This is my first file\n'

>>> f.readline()
'This file\n'

>>> f.readline()
'contains three lines\n'

>>> f.readline()

Finally, the readlines() method produces a list of the file’s remaining lines. When the end of the file (EOF) is reached, all of these reading methods return empty results.

>>> f.readlines()
['This is my first file\n', 'This file\n', 'contains three lines\n']

Python File Methods

With the file object, you may use a variety of methods. Some of these were utilized in the examples above.

Here is the complete list of methods in text mode with a brief description:

close()Closes an opened file. It has no effect if the file is already closed.
detach()Separates the underlying binary buffer from the TextIOBase and returns it.
fileno()Returns an integer number (file descriptor) of the file.
flush()Flushes the write buffer of the file stream.
isatty()Returns True if the file stream is interactive.
read(n)Reads at most n characters from the file. Reads till end of file if it is negative or None.
readable()Returns True if the file stream can be read from.
readline(n=-1)Reads and returns one line from the file. Reads in at most n bytes if specified.
readlines(n=-1)Reads and returns a list of lines from the file. Reads in at most n bytes/characters if specified.
seek(offset,from=SEEK_SET)Changes the file position to offset bytes, in reference to from (start, current, end).
seekable()Returns True if the file stream supports random access.
tell()Returns an integer that represents the current position of the file’s object.
truncate(size=None)Resizes the file stream to size bytes. If size is not specified, resizes to current location.
writable()Returns True if the file stream can be written to.
write(s)Writes the string s to the file and returns the number of characters written.
writelines(lines)Writes a list of lines to the file.

Leave a Comment

Your email address will not be published.