File Manipulation In Python: Open, Read, Create, Write, Append

masterjosh
by
masterjosh
Apr 04, 2019
File Manipulation In Python: Open, Read, Create, Write, Append

One of the key features of most object oriented programming languages is their ability to manipulate files. This involves creating files, reading from them, opening them, writing to them, and appending text (or data) to them.

In this article, we take a look at how to do file manipulation in Python.

For simplicity, we will be working with text files here. While OOP languages can manipulate many different kinds of files (all kinds in fact), text files are best suited for an introductory article.

Python supports file manipulation out of the box, without needing any external libraries. So if you have the latest version of Python installed on your machine, you can easily run the code pieces shared here to see how they actually work.

The Python Open() Function

In order to perform any file handling operation (read, create, write, or append), you must first “open” a file using Python’s open() function.

The open function has an optional mode argument. Even though it is optional, you usually need to supply it in most cases.

Reference Table Of The Different Modes For Opening A File In Python

Before proceeding, let me drop this table here to help the reader understand the code pieces that will be shared. Also, this table should serve as a reference table on Python file open modes for those who work with Python file handling frequently.

Mode Description
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer is placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens 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.
w+ 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.
wb+ 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.
a Opens 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.
ab Opens 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.
a+ 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.
ab+ 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.

I have also found this diagram to be a pretty useful (albeit incomplete) summary:

"Python File Modes"

Ok, now that we have some background information on file open modes, let’s start actually manipulating files with Python code.

Creating Files In Python

To create a text file, use the following command:


fhandle = open("masterjosh_example.txt","w+")

In the snippet above, a variable, fhandle is declared and the result of the open()function is assigned to it. In this case, our open() function is given two arguments – the name of the file we want to create, and a string that represents the type of operation or permission that we want.

We’re using “w+” so our file will be opened for both reading and writing. If the file already exists, it will be overwritten. If not, a new file will be created.

The location where the new file is saved depends on your Python setup. If you’re using an IDE like Visual Studio for Python development, your file will be saved in the root folder of the project you’re working on. In other cases, you should be able to find the newly created file in the Python installation directory of your machine.

If you have trouble finding your file on Windows, try checking C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32

Writing To A File

To write to the file you just created, use the following snippet:


fhandle.write("Hello World!")

fhandle is the variable in which we stored the output of our previous open()command. The write() function accepts the text we wish to write as an argument. In this case, “Hello World!”

 

To close the file and save your changes, use:


fhandle.close()

We can also write multiple lines to a file by making use of a loop. Take a look at the following snippet.


for i in range(5):
    fhandle.write("This is line number %d\n" % (i+1))

In this case, we used a for loop. We set the number of times the loop should run to 5, and formatted the text to display integers. Again, to close the file and save your changes, use:


fhandle.close()

After running the code pieces above, you can open the file and see the contents.

It should look like this:

"Writing To File In Python"

Appending Text To A File

In the previous section, we wrote to a blank file. Now, we want to append text to the file we created previously which already contains some text. To do this, we need to open the file in append mode.

 

In this case, I will be using “a+” as the file open mode.


fhandle = open("masterjosh_example.txt", "a+")
fhandle.write("This is an appended piece of text")
fhandle.close()

Run the code and open the file (ehi_kioya_example.txt) to view it and it should now look like this:

"Appending Text To A File In Python"

Reading A File

To read the contents of a text file, just open the file in read mode by setting the modeargument to “r”. It is also a good idea to confirm that you’re indeed in read mode before attempting the actual read. And with the print command, the entire contents of your file will be printed to screen.

Here’s how the code for this looks:


fhandle = open("masterjosh_example.txt", "r")
if fhandle.mode == 'r':
    content = fhandle.read()
    print(content)

"Reading Text Files In Python"

If you want to process the contents of the file line by line, just use the readlines()function. The code would be structured like this:


fhandle = open("masterjosh_example.txt", "r")
if fhandle.mode == 'r':
    linebyline = fhandle.readlines()
    for line in linebyline:
        print(line)

"Reading Text Files Line By Line In Python"

As you may notice from my screenshots above, I use Visual Studio for Python development and highly recommend it too especially if you’re a .NET developer or intend to play with multiple programming languages.

If you have any questions about file handling in Python or a comment about any of the examples discussed here, just leave a comment using the comments box below.




Add Comment