How to Automate JSON files and text files using Python

In the DevOps world, you are continually parsing, searching, and changing the text in files, why not automate them using Python. Lets learn how to automate files using Python with few lines of code.

Reading and Writing a text file using python

Using Pathlib module

Pathlib module: This module offers classes representing filesystem paths with semantics appropriate for different operating systems.

In below code snippet you will use pathlib module to open a test.txt file from defined path and then read the content using path.read_text() function and then stores it in the variable cont. Next, using path.write_text() it appends the data in the same file. The Path.write_text() function opens the file in text mode, write data to it, and close the file. Lastly using print() function it prints the value of both cont and cont1.

Lets learn to read and write into file using pathlib module with following example:

import pathlib
from pprint import pprint

path = pathlib.Path("test.txt")
cont = path.read_text()
cont1 = path.write_text("LOG:information technology")
print(cont)
# cont1 provides the number of indexes used in the text "LOG:information technology"
print(cont1)  
O/P:

Hi
26

Using open() function

With open() function you open the file to perform the operations such as read, append data or close the file etc. Here as soon as file is opened , it reads the file using read() function and stores the values in the content variable. Next, it prints the content and the length of content inside the file using print() and finally closes the file.

myfile = open('test.txt')
content = myfile.read()
print(content)
print(len(content))
myfile.close()   # Always close your file so that it doesnt consume memory
O/P:

LOG:information technology
26

Lets look at another example where mode is defined as r and w which denotes read and write . We can also use a to append the data in file. The Difference between w and a mode is w overwrites the data in file and a appends the data in file.

Also, If you intend to open any binary file such as pdf use the mode as rb or wb which denotes read or write binary.

In below example first file is opened and then read using read() function and prints the content. Next, you write the data in the file “Adding new line” in the file using write() function

with open("test.txt",mode="r") as mynewfile:   
    contents2 = mynewfile.read()
    print(contents2)

with open("test.txt",mode="w") as mynewfile:  
    contents1 = mynewfile.write("Adding new Line")
O/P

LOG:information technology

Reading and Writing a Json file using python

Using json.load() and json.dump()

  • Create a file and name it as file.json and paste the below content
{
 "a": 1,
 "b": 2
}

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. Json module helps to work with JSON files such as reading a JSON file, writing data in JSON file or appending the file etc. json.load() function is used to open a JSON file. json.dump() function will add data into the JSON file.

Next , create another file and name it as program.py. Copy and Paste the below code. Here you will use json and pprint module along with open() function. Firstly you will open and read a JSON file using json.load() then Updating the file but only in outputs not in actual memory and finally Updating the file in outputs as well in actual memory using json.dump()

import pathlib
import json
from pprint import pprint

# Section 1: Opening and Reading a JSON file using json.load() 

with open('service-policy.json', 'r') as opened_file: # Opens a Json File
    policy = json.load(opened_file) 
    print(policy)
    pprint(policy)

# Section 2: Updating the file but only in outputs not in actual memory

policy['c'] = '3'  
pprint(policy)

# Section 3: Updating the file in outputs as well in actual memory using json.dump()

This way you will update the policy actually in memory

with open('service-policy.json', 'w') as opened_file1:
    policy2 = json.dump(policy, opened_file1)
print(policy2)

The output of the above program is as follows:

Conclusion

In this tutorial you learnt how to read and write text in text files and JSON files. Automating with Python is a fun and takes few lines of code and also keeps you away from silly manual errors. With that now, you have idea to automate files in python , what are you going to automate with Python?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s