Chapter 8: Python vs the Zombies

Start Here
Chapter 1: In which a cow surprises you

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings

Chapter 4: More on strings and donkeys

Chapter 5: Crooks R Us Bank & Lists

Chapter 6: Orc functions

Chapter 7: Dictionaries, Locks, and Gobbledygook

Chapter 8: Python vs the Zombies (This chapter)

Chapter 9: The Doggy-Nator and List Comprehensions

Chapter 10: In which you Eval the Doggy

 

Attack of the Sexy Alien Vampire Zombies (yes, all of them)

You land in Iceland, to be immediately attacked by a large number of Sexy Alien Vampires Who Are Also Zombies (called SAVAAZ from now). They were created when many Twilight fans fell into a radioactive vat, and were then kidnapped by time travelling aliens. The Twilight fans became zombies and bit the aliens, who also became time travelling sexy alien zombies, creating the SAVAAZ. And now, they all want a piece of you.

“Brains. We want brains. We don’t have any, cause we read vampire romance fiction. Bargh!”

“What do we do?” you whimper in fear.

“You need to use your Geeky Chosen One skills to cast a spell. The top secret, super powerful, Expecto zombiem spell.”

#

Python is a great replacement for shell scripts. With Python, you can use all the cool libraries and inbuilt functions, rather than having to struggle on the command line.

The example below is for Linux only, as the command line is rarely used on Windows.

Create a file called zombies.txt, and add the following text to it: “Expecto Patronum Zombiem Madeup Wordup”

Start up a command line Python shell:

This may require some explanation. The import keyword brings in external libraries. In this case, it is the subprocess library, used to (you guessed it), spawn processes. POpen() creates a new process instance. In this instance, we are running the command “ls -al”. Due to the way it works, you cannot have any spaces in the command. So each part of the command has to be passed into an array. That is why we are doing:

It is possible to do: Popen(“ls -al”), if you set the shell=True parameter. However, that creates a security hole, which is why it is discouraged. See here

The stdout=subprocess.PIPE, stderr=subprocess.PIPE means the output is piped to the subprocess module. This is needed so we can capture them.

So now we have an instance p, that can run a command line program. How do we call it?

out, err = p.communicate()

p.communicate() reads the data from the command, and stores in in out and err, the pipers for stdout and stderr.

In the example below, err is empty, as there was no error. Let’s try an error. If we try to run the command “ls -z”, which shouldn’t work, as there is no “-z” flag.

This time, out is empty.

That worked. Now, let’s read the file we created earlier. We could just read it directly, but let’s read it from the command line.

This is a very brief introduction to a subject on which whole books can be written. But hopefully, I have given you enough info to get started. If you want to see anything else, leave a comment.

 

The SAVAAZ run away screaming. “Nooooo! We can’t mess with him. He knows Python. Arggghh!”

Leave a Reply