Reading and Writing from Files with Python


As you may have read, I recently began getting started with programming for the sake of furthering my personal networking skills. I started working on basic scripts using NetMiko and thinking of ideas on how I could use this for future projects. One of the things that I thought of was around general device management. For instance, what if I wanted to pull current code versions from all of my switches? I would need a list of my switches by management IP and then connect to each of them. This led me to start looking into how to read and write from static files with python, since that is the language I am using. So that’s where I am and maybe where you are too. If you are, here’s what I found on how to accomplish this task.

Opening a File

Whether you are reading or writing to \ from a file, you need to open it first. There is a very simple, single line to do this:

variable = open("filename","mode")

You create a variable and then add the contents of the file to this object. Notice the option for “mode” in that command. There are actually 4 common modes to use with this function:

  • “r” – Read mode
  • “w” – Write mode
  • “a” – Append mode – adds info to the end of an existing file
  • “r+” – Read and Write mode

Writing to a File

This post will cover a very simple form of writing to a file from a python script. First thing we are going to do is open a new file in write mode:

newfile = open("newfile.txt","w")

From here there is a python function of “write” that is used to add the desired text to the new file. Adding the “\n” at the end of each line allows you to create separate lines in the text file as well:

newfile.write("This is a list of switch management IP addresses.\n")

Lastly we just need to close the connection to this new file that we were working with:


Once you create that script and execute it, you are left with a new file called “newfile” that you can view with the text you chose:

Reading a File

This is the part of this post that is the most important to me. I envision having a few different files full of management IP addresses for my network. I might have a file called “firewalls”, another called “routers”, and another called “switches”. Each is a list of IP addresses my script could run through to gather data or make changes.

Starting the way we did before, I need to open the text file I made in read mode:

newfile = open("newfile","r")

From here I can go ahead and read the contents in one of three (common) ways:

  1. The whole file at once
  2. A select amount of characters
  3. A specific line number

To read the whole file at once, you simple “print” that file to get the output:


To read a specific number of characters, the command is the same with one change:


The above script will print the first 8 text characters in the file.

Lastly, you can select the specific line to print by using readlines() and then adding the line number after in brackets:

print newfile.readlines()[2]

That will print out the second line of text in the file.


Now think of the possibilities and uses for this. I am specifically thinking of making a list of commands, creating a loop with a counter, and then using “print newfile.readline()[2]” to loop through a file line by line while executing commands on every switch for instance. Sure this is basic code at this point still, but you can see how the usage can become important later on!


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.