Reading And Writing Files With Python


Jut about any program of a decent size needs to be able to read and write from files. At very least, it needs to be able to generate logs.

Python is also tightly integrated into Linux system administration and scripting. Again, reading and writing are important for managing a system.

Opening A File

Python creates and object from the file that it opens. It can then manipulate that object. When it’s done, it uses the object to save the any changes back to the file.

Create a new text file and fill it with nonsense from your text editor. Be sure to include multiple lines of text.

Now, create a Python file to work in.

In that file, you can use Python’s open function to open the file and create an object with it.

file = open('test.txt', 'r+')

file is now an object that holds the information on the file. It can be both read and written.

As you can see, the open() function takes two arguments. The first is the path to the file that needs to be opened, and the second is the mode that it will be opened in. There are a number of modes, but there are only a few that you’ll use frequently.

Read r Only read from the file

w Erase and write to a file
Append a Add new lines to the end of a file
Read & Write r+ Read and write to a file without overwriting it
Write & Read(Overwrite) w+ Write and read a file, overwriting its contents

Reading A File

Take your file object for a test drive. Add a line in that calls the read() method.

The read method outputs the entire contents of the file into the command line. It’s similar to using cat. Reading the file shows each line as a string with a newline character at the end. The last line appears as an empty string, ''.

There is another method that you can use to grab individual lines, the readline() method. readline() starts at the beginning of a file and reads each line out individually each time it is called.

Try removing the read() line from your file and calling readline() multiple times instead.

for x in range(1, 11):

If you had less than ten lines, you probably noticed a bunch of blank strings. That’s what readline() spits out whenever it goes beyond the number of lines in a file.

Writing A File

Writing to a file is even easier. All that you need to do to write to a file is call the write() method and pass it the string that you want written to the end of the file. write() will always write new lines to the end of the file.

file.write("I'm a new line!")

You can write to a file as many times as you need to. Each time the write() method is called, it creates a new line, making it ideal for logging.


As of now, you don’t have any real control of the flow through the file. The seek() method gives you some of that back.

It takes two numbers as arguments and allows you to move through the file in both directions. The first number is the position that you want the invisible pointer in the file to reverence. If you pass in a three, it’ll be located after the third character. The second number is the place where you want it to start from. A value of 1 in the second place will cause the method to seek from he pointer’s current position. A 2 would be then end of the file.

Try using seek() and readline() together., 0)

Python will print out from fifteen characters in to the end of the line where that point falls.

Closing the File

When you’re done working with a file, you have to close it in order for changes to be applied and for memory to be freed up. You can cloe a file by calling the close() method on the object.


Closing Thoughts

Now you can use Python to collect information from a file. You can also write output to a file. This opens up plenty of opportunity to use and store information from text files.


  1. Create a text file with at least 10 lines.
  2. In Python, open the file as both readable and writable without overwriting it.
  3. Read the file in Python.
  4. Reset the invisible pointer in the file to the beginning.
  5. Read only the first two lines of the file.
  6. Reset to the beginning again.
  7. Read only the seventh and ninth lines.
  8. Write two new lines to the file.
  9. Close your file.

Comments and Discussions
Linux Forum