Python – Kirk Byers Course Week 1 Part 1
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.
daniel@daniel-iperf3:~/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:
daniel@daniel-iperf3:~/python/Week1$ python3 stdin.py 1.txt 2.txt 1 2 3 4 5 6 7 8 9 10 daniel@daniel-iperf3:~/python/Week1$ cat 1.txt 1 2 3 4 5 daniel@daniel-iperf3:~/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 Continue reading