As a network engineer we need to deal with thousands of devices at once and this is the essence of network automation. The code snippet below will give you the entry point into how to read files in bulk with python for network engineers. Please note, this is not the only way of doing things but this is the most simplistic way of introducing the concept of network automation to absolute beginners.
Real example of how to read files for network engineers with python. This is a part of series of python for network engineers.
I am going to take an example of reading multiple text files each containing show version from different devices.
- Printing all files inside a particular directory.
- We are going to leverage python’s os module.
- Import the in-built os module and call the listdir method.
- listdir method takes argument of the path of the directory relative to where you have placed your script file that you want to scan for files inside it.
import os
for file in os.listdir('input'):
print(file)
╰─ python3 script1.py ─╯
rtr-012-ce01.txt
rtr-012-ce02.txt
rtr-039-ce02.txt
rtr-039-ce01.txt
rtr-017-ce01.txt
2. Opening all files and reading content inside the directory.
- For each file in input directory
- open the file in read-only mode and create a file handler object as f
- read data from file handler object into a variable called data
- print the data
import os
for file in os.listdir('input'):
with open('input/' + file, 'r') as f:
data = f.read()
print(data)
3. Once you have retried the contents of the file into any variable, data in this case. We can write the script further to extract what we need exactly from the data.
- Extract details like
- Hostname
- Hardware
- Version
- anything else that you want to extract
This is the bird’s eye introduction on how to read files in bulk with python. The next post in series is going to talk about how to process the data we have read into meaningful information.
Read next post here. How to parse data using python
1 thought on “How to read files in bulk with python for Network Engineers”