Python: Reading & Writing to Same File

Python makes it easy to read & write to files with the help of built-in functions. In this article, we will look at how to read & write text files that consist of individual lines of data separated by newline character (\n).


Python: Reading & Writing to Same File

Here is the syntax to read a file in python.

file_object=open("path/to/file",access_mode)

In the above open command, you need to provide the file path to your text file and also mention the access mode to process its data. If you don’t mention the full file path, then python will look for the file in present directory. The open command returns a file object. Here are the access modes available:

  • r – read only
  • r+ – read and write
  • w – write only
  • w+ – write and read
  • a – append to existing data
  • a+ – append and read

Also read : How to Find Largest Files & Folders in Linux


Now we will look at the most common use cases with files in Python.

Open a file in Python

Here is an example to open file for reading and writing

fd = open("/home/ubuntu/test.txt","r+)

Close a File in Python

Here is the command to easily close an open file in python

fd.close()

Also read : How to Run Linux Command in Background


Read and Write File in Python

Here is how to read and write file in Python.

fd = open("/home/ubuntu/test.txt","r+)
fd.read()
'Test data'
fd.write(' analysis')
fd.close()
fd = open("/home/ubuntu/test.txt","r+)
fd.read()
'Test data analysis'

Also read : Show Commands with date & time in Linux history


Different ways to read a file in Python

There are three ways to read a file in python.

1. read([n]) – allows you to read the number of bytes specified in the command. If no number is specified then it reads the entire file

2. readline([n]) – reads one line of the file up to n bytes. If no number is specified then only the next line is read. Even if n is more than the length of the line, it will read only 1 line.

3. readlines() – read entire file line by line and returns the entire file as a list of strings, where each line is a returned as a single string.

Also read : How to Use Linux History Command


Different ways to write a file in Python

There are a couple of ways to write to a file in Python.

1. write() – insert a single line of text to the file.

2. writelines() – write a list of strings to the file.


Here is an example that showcases all the above functions.

>>> file1 = open("test.txt","w")
>>> L = ["First line \n","Second line \n","Third line \n"]

>>> file1.writelines(L)
>>> file1.write("Fourth Line \n")
>>> file1.close()
>>> file1 = open("test.txt","r+")
>>> print "Output of Read function is "
Output of Read function is
>>> print file1.read()
First line
Second line
Third line
Fourth Line


>>> file1.seek(0) #takes file reader back to the beginning of file
>>> print file1.read(5)
First
>>>
>>> file1.seek(0)
>>> print file1.readlines()
['First line \n', 'Second line \n', 'Third line \n']
>>> file1.seek(0)
>>> file1.readline()
'First line \n'

That’s it. As you can see it is very easy to work with files in Python.