Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu

How to read files in bulk with python for Network Engineers

Posted on July 29, 2021August 15, 2021 by Gurpreet Kochar

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.

directory structure of the script.
python for network engineers
Parent directory structure ⬆
Input folder has all the text files we need to scan
I have placed all the text files with command outputs in input directory. You can create any directory structure you want.
  • 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

How to parse data using python
https://networkautomationlane.in/how-to-parse-data-using-python/

Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

1 thought on “How to read files in bulk with python for Network Engineers”

  1. Pingback: How to parse data using python – Network Automation

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...
 

    %d