This post is the first one going through the Kirk Byers Python for Network Engineers class.

In the first class Kirk shows how to pipe data into Python by using the module fileinput with the following code(modified for Python3):

import fileinput

for line in fileinput.input():
	print(line.split("."))

I’ll show what this script outputs and then we’ll look at the code.

[email protected]:~/python/Week1$ echo "192.168.1.1" | python3 stdin.py
['192', '168', '1', '1\n']

How did we get data into Python? We used “echo” to send data to stdin (standard input). The function “fileinput.input()” can take either files as arguments or if no files are listed it will read from stdin.

It’s possible to use “fileinput” to read from several files and print out the content. We used the following code:

import fileinput

for line in fileinput.input():

Then we print out the text:

[email protected]:~/python/Week1$ python3 stdin.py 1.txt 2.txt 
1

2

3

4

5

6

7

8

9

10

[email protected]:~/python/Week1$ cat 1.txt
1
2
3
4
5
[email protected]:~/python/Week1$ cat 2.txt
6
7
8
9
10

Here we printed the contents of two files by sending them to “fileinput” We can see what arguments we provided to “fileinput” byt slightly modifying our code and printing out “sys.argv”.

import sys
import fileinput

for line in fileinput.input():
        print(line)

print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2])

This is then the result that we get:

[email protected]:~/python/Week1$ python3 stdin.py 1.txt 2.txt 
1

2

3

4

5

6

7

8

9

10

stdin.py
1.txt
2.txt

The first argument is the script name “stdin.py”, the second argument was the file “1.txt” and the third argument was the file “2.txt”. This is zero indexed meaning that the third argument is represented by the number two.

If we go back to the original script. What type is the data we sent in? Let’s modify the script a bit again.

import fileinput

for line in fileinput.input():
        print(line)
        print(type(line))

Then we run the script and see what Python gives us back.

[email protected]:~/python/Week1$ echo "192.168.1.1" | python3 stdin.py
192.168.1.1


Python automatically handles types for us and decided this was a string.

Let’s go back to the orginal code again.

import fileinput

for line in fileinput.input():
        print(line.split("."))

We can send several strings into the script if we want to.

[email protected]:~/python/Week1$ echo "192.168.1.1" "192.168.2.2" | python3 stdin.py
['192', '168', '1', '1 192', '168', '2', '2\n']

What can we do if we want to get rid of the trailing newline? First I tried using the function “rstrip()” but it’s not possible to use “rstrip” on a list. We modify our script once again:

import fileinput

for line in fileinput.input():
        print(line)
        print(type(line)) 
        line_split = line.split(".")
        print(line_split)
        print(type(line_split))
        print(type(line_split[3]))
        line_split[3] = line_split[3].rstrip()
        print(line_split)

This code first prints the unmodified line which is a string. Then we split the line and store the parts in a list. We print the list including the trailing newline at the end. The list consists of strings where the last string is “.1\n”. We reference this string with list slicing by referring to it as “line_split[3]”. We run the function “rstrip()” to remove the “\n” from the last string in the list and then print out the contents of “line_split”. This is what it looks like when running the script:

[email protected]:~/python/Week1$ echo "192.168.1.1" | python3 stdin.py
192.168.1.1


['192', '168', '1', '1\n']


['192', '168', '1', '1']

This was a lot of text for a few lines of code but my goal as I go through these exercises is to learn myself and then hopefully pass along something as well. In summary we used the function “fileinput()” to read from “stdin”. This was input into Python as a string and then we printed out the contents of the string. We wanted to remove a trailing newline on a string that was part of a list by using the function “rstrip()” but it’s not possible to run “rstrip()” on a list. We had to refer to the individual string in the list by using list slicing and then running “rstrip()” to remove the “\n” from the string.

Since I haven’t used Git before I’m going to upload the code via Git. I’m using the Git client for Windows. First I need to clone the repo:

PS C:\Python> git clone https://github.com/ddib-ccde/pyforneteng
Cloning into 'pyforneteng'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
PS C:\Python> dir


    Directory: C:\Python


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2017-02-10     14:49                pyforneteng

I have stored my script in a folder called “Week1”. I’m going to add this folder to my repo:

PS C:\Python\pyforneteng> git add Week1
PS C:\Python\pyforneteng>

Then I need to commit and push the update:

PS C:\Python\pyforneteng> git commit
 1 file changed, 12 insertions(+)
 create mode 100644 Week1/stdin.py

PS C:\Python\pyforneteng> git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 608 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)

Thanks for reading and see you in the next post!

Python – Kirk Byers Course Week 1 Part 1

Leave a Reply

Your email address will not be published. Required fields are marked *