Geek Culture
Published in

Geek Culture

File Handling In Python

How to Read, Write, and Append a Python File?

File Handling: Python Tutorial Series

Hello Learners..!!

In the last section of this Python Tutorial Series, we talked about how to solve the Tower of Hanoi problem using Python. In this article, we are going to deal with Python files.

First, let’s understand why do we need file handling in Python. Because the program that we developed so far takes data from the user in an interactive manner. Such data remain in memory only during the lifetime of the program. But what if we want to store the data permanently. For example, we developed a Python program for a login system. The user interactively enters the username and Password. But here we have to store this data permanently in a file. Python provides a standard input file and a standard output file to deal with the transitory data. The standard input file is read from the keyboard, and the standard output file is displayed on the screen. Apart from these, we can also create a Python file and store data permanently. So, let’s see how to deal with these issues.

File Handling

Files provide a means of communication between the program and the outside world. A file is nothing but a stream of bytes, comprising data of interest. We perform three operations with a file, i.e, read, write, and append. But before that, we need to open the file. Python provides a built-in library open() for this purpose. The syntax is as follow:

a = open(file_name, access_mode)

The open() function takes the name of the fila as the first argument. The second argument indicates the mode for accessing the file.

  • read
a = open(file_name, 'r')

read (r) is used to read the existing file. If the specified file doesn’t exist, then it will display an error. For example, I created a file ‘aboutme.txt’ in a directory. If I read this file then it will give a valid output. But if I try to read the non-existing file, then it will show an error:

Read a file in Python
  • write
a = open('filename', 'w')

Write mode is used when a file is to be accessed for writing data in it. Here, if the specified file doesn’t exist, then Python will create a new file. However, while opening a file in write mode, if the file already exists then the file gets overwritten. For example:

>>> c = open('newfile.txt', 'w') #it will create a new file in the             directory
>>> c.write("I write articles on Medium.")
write a file in python

When you run the first command, the Python will create a new file(newfile.txt) because it didn’t exist. After writing something in the file, you must close the file to save the file in the directory. After you close, you can’t read the file. You have to write the command to read the file.

  • append
a = open('filename', 'a')

As the name suggests, append mode allows us to write into a file by appending contents at the end of the specified file. Just like write mode, if the specified file does not exist then it will create a new file.

Append a file in Python

Now, suppose if we wish to copy the content of a text file, say, ONE.txt in another file TWO.txt. For this purpose, we open the file ONE.txt in read mode and the output of the file TWO.txt( yet to be created) in write mode, read the data from the file ONE.txt, and write it into the file TWO.txt.

>>> a = open('ONE.txt', 'r')
>>> b = open('TWO.txt', 'w')
>>> data =
>>> b.write(data)
>>> a.close()
>>> b.close()

Note that if an application requires the file to be opened in both read and write mode, “r+” mode can be used while opening it.

That’s it for this article. If I missed something then let me know in the comment section.

If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.

Thanks for reading.

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store