File Handling in Python

Discover everything you need to know about file operations in Python. Our comprehensive guide covers file handling, reading, writing, and working with files in Python
E
Edtoks3:47 min read

Python supports file operations like any other programming language. File operations are a built-in feature of Python.

Common file operations create, read, write, append, close and delete files.

Open the file

To perform any file operations the first file should be open. This can be done open function. After opening you can read, write or append the data to the file.

Syntax for open is open(filename, mode, encoding, ..), other parameters are not so important. 

File modes

  Mode Description
  'r' open for reading (default)
  'w' open for writing, truncating the file first
  'x' Create a new file and open it for writing
  'b' binary mode
  'a' open for writing, appending to the end of the file if it exists
  't' text mode (default)
  '+' Open a disk file for updating (reading and writing) ('r+' or 'w+')

Read from the file

Open the file in read mode 'w' to read the file.

The below code shows how to read a file. make sure after reading close the file.

Create a test file

echo "This is test file" > test.txt
echo "This is second line" > test.txt
echo "This is third line" > test.txt
cat test.txt

Now open the test.txt file and check 

readfile = open('test.txt', 'r')
data = readfile.read()
print(data)
readfile.close()

Output is

This is test file
This is second line
This is third line

 Important Note: If a file does not exist then it throws an exception, eventually, the application crashes if the exception is not handled.

You can read the file line by line with the readlines method

for line in readfile.readlines():
    print(line)
readfile.close()

Write to the file

Open the file in write mode 'w' to write the file.

Here if a file does not exist Python creates a completely new file and if exists, then it will overwrite the previous content and write completely new data.

The below code shows how to write the data to a file

writefile = open('test.txt', 'w')
writefile.write('Writing new line')
writefile.close()

There is no output on the terminal, but you can check the file content.

cat test.py

Append to the file

As said above opening the file in write mode deletes the file contents if exists, To avoid overwriting file content open the file in append mode.

In append mode, new data will be appended to the end file if the file exists and create a new file if does not exist.

# Appending new data to the existing file.
writefile = open('test.txt', 'a')
writefile.write('\nWriting second new line')
writefile.close()

# Creating new file with append mode
writefile = open('temp.txt', 'a')
writefile.write('Writing new line')
writefile.close()

Here also no print statements hence no output, but you can check the file contents

cat test.py
cat temp.py

Close the file

Each time you open a file, make sure to close it as shown in the sample above.

Most of the users may forget to close the file. But Python supports a auto close with a with and as statement.

with open('test.txt', 'r') as readfile:
    data = readfile.read()
    print(data)

Output is

Writing new line
Writing second new line

Similarly, you can file in other modes as shown in the above table.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.