Read and Write data from external file in Python

How to read and write data from an external file in python. Reading and writing data are very important while you working on any project or application with any programming languages. So Let’s see how to handle files read and write operations using python.

How to Read File in python

Suppose you have a file, one thing you might do with it is seeing its substance(contents). In this topic, you’ll learn various ways how to read files Python. To read the file first we have to open it in the reading mode.

f = open("languages.txt", "r")
print(f.read())

Since now the file is opened, how does the real reading part go? We should initially choose a document that we need to read. So, imagine we have a record called ‘languages.txt’ that resembles this:

To read the file you can:

  • use the read(size) method
  • use the readline(size) method
  • use the readlines() method
  • iterate over the lines with a for loop.

The initial three different ways are special file object methods while the last one is a general Python loop. How about we go over them individually.

read()

read(size) reads the size bytes of a file. If the parameter isn’t specified, the entire document is read a single variable. Hence, this is the thing that we’ll get if we apply it to our file:

f = open('languages.txt', 'r')
print(f.read())
f.close()

Output

readline()

readline(size) is like read(size) yet it reads size bytes from a single line, not the entire file. Lines in files are separated by newline escape sequences: ‘\n’, ‘\r’ or ‘\r\n’. We’ll choose ‘\n’ in this point. However, remember that this escape sequence depends on your operating system.

We should continue with our example. The document ‘languages.txt’ contains 5 lines. Here is the thing that we’ll get if we try 3 bytes from each line:

f = open('languages.txt', 'r')
print(f.readline(3))
print(f.readline(3))
print(f.readline(3))
print(f.readline(3))
print(f.readline(3))
print(f.readline(3))
f.close()

Output

As obvious, the output doesn’t contain the initial three characters from every one of the 6 lines. This is because when we indicate the size parameter, we get size bytes from the line until it ends and at exactly that point go to a different line. The newline character is considered as a part of the line here. In this way, in our example, we need two passes of readline(3) to read the first line which has three characters and a newline character (4 bytes altogether). This is the place each one of those vacant lines in the output originates from.

readlines()

readlines() allows us to read the whole file as a list of lines. Here’s what it looks like:

f = open('languages.txt', 'r')
print(f.readlines())
f.close()

Output

for loop()

The most efficient way to read the contents of a file is to iterate over its lines with for loop.

f = open('languages.txt', 'r')
for lines in f:
    print(lines)
f.close()

Output

This is the best way to read large files because we can just work with one line at a time or work with specific lines and discard the rest.

Hence, we can conclude:

  • read() method reads the file as a whole
  • readline() reads the file one line at a time
  • readlines() reads the file as a list of lines
  • the best way, however, is to use for loop to iterate over the lines of the file

What is Writing File?

One significant skill for software engineers is realizing how to create new files or add data to existing ones.

How to write data into a file in python

All things considered, the initial step of writing a file in Python is, obviously, opening said file for writing. The essential mode for writing is ‘w’ which permits us to write text to the file. There are a couple of things we have to focus on. To begin with, this mode permits us to make new files. This happens when the document we’re attempting to open doesn’t exist yet. Second, if the file as of now exists, its contents will be overwritten when we open it for writing.

Since the file is open, we can utilize the write() method. file.write() permits us to write strings to the file, different kinds of data should be changed over to a string previously. Let’s see how it works.

file = open('languages.txt', 'w', encoding='utf-8')
file.write('This is the new test file!')
file.close()

Now, let’s try to open the file

f = open('languages.txt', 'r')
print(f.read())
f.close()

Output

Note that the strings are written to the file exactly as they are. When we call the write() method several times, the passed strings get written without any separators: no spaces, no newlines, just strings combined together into one.

Writing multiple lines

If we need the file to have multiple lines, we have to indicate where the ends of the lines should be. Lines in files are separated by newline escape sequences: ‘\n’, ‘\r’ or ‘\r\n’. We’ll choose ‘\n’ in this point. However, remember that this sequence relies upon your operating system.

Assume, we have a list of names and we want to write a file, each on a new line. This is the manner by which it should be done:

name1 = ['John', 'Alexa', 'Rose', 'Henry']
 
file_name = open('names.txt', 'w', encoding='utf-8')

for name in name1:
    file_name.write(name + '\n')
file_name.close()

If we print the file now, we will get

As we can see, our file has four lines with the four names from the list. If we need the names to be on the same line separated by whitespace, we would do it similarly, however rather than \n, we would include a space.

Another strategy for composing the records is file.writelines(). writelines() takes an iterable sequence of strings and writes them to the file. Much the same as with write(), we have to determine the line separators ourselves. This is the means by which we could’ve composed the names.txt document utilizing this technique:

names = ['John\n', 'Alexa\n', 'Rose\n', 'Henry\n']
 
file_name = open('names.txt', 'w', encoding='utf-8')
 
file_name.writelines(names)
 
file_name.close()

On printing the file contents we will get:

In the end, we got the same file as before, the only difference is that the original strings had to come with the separator.

Append to file

The ‘w’ mode works completely fine if we wouldn’t care if anything gets erased from the current file. In any case, we need to add a few lines to the file, not overwrite it totally. How might we do that?

All things considered, we can utilize the ‘a’ mode which represents add. As you would have speculated, this permits us to compose new strings to the file while keeping the current ones.

Assume, we need to add the name Rachel to the names.txt.

Here’s the manner by which we can do that:

file_name = open('names.txt', 'a', encoding='utf-8')
 
file_name.write('Rachel\n')
 
file_name.close()

Now, let’s try read the file and then print it.

name_files = open('names.txt','r')
print(name_files.read())

Output

Hence, we can conclude:

Depending on whether we want to keep the original contents of the file or not, we can use the mode ‘a’ or ‘w’ respectively. The actual writing can be done with write() or writelines() methods.

Let’s try to solve a problem:

Create a file planets.txt and write the names of the Solar system planets there, each on a new line. In total, the file should contain 8 lines with the following planets: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.

When opening the file, specify encoding=’utf-8′.

planets = ['Mercury\n', 'Venus\n', 'Earth\n', 'Mars\n', 'Jupiter\n', 'Saturn\n', 'Uranus\n', 'Neptune\n']
planets_file = open('planets.txt', 'w', encoding='utf-8')
planets_file.writelines(planets)
planets_file.close()

Output