Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr0675 :: Python Response to Bad Apples Podcast 5x18

A response to a challenge from Klaatu - translate a bash script into python

<< First, < Previous, , Latest >>

Thumbnail of Doug Farrell
Hosted by Doug Farrell on Friday, 2011-03-04 is flagged as Explicit and is released under a CC-BY-NC-SA license.
python, bash, scripting. 2.
The show is available on the Internet Archive at: https://archive.org/details/hpr0675

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:07:47

general.

In episode 5X18 of the Bad Apples podcast, Klaatu challenged me to create my own podcast explaining my Python version of his bash script. His bash script created a list of files that matched a file name pattern, then read the first line from each of those files and wrote that to an output file. My Python program does exactly the same thing, but in Python.
Here is the body of that program with the comments stripped out:

#!/usr/bin/python
import glob
outfile = open("toc.output", "w")
for filename in glob.glob("*.txt"):
    outfile.write(open(filename).readline())

The above text can be used to follow along with the audio of the podcast. Here is the English explanation version of the above program:

  • Tell the system the rest of the text in the file should interpreted by Python
  • Import the glob module, which is one of the library modules that comes with Python
  • Create a new file object called "toc.output" that we can write to
  • Iterate over the list of files that match the pattern "*.txt" created by the glob function, and assign each matching file in turn to the filename variable
  • Open each filename, read the first line from the file and write it to our previously opened output file.
It's not shown above, but each matching filename that we open is closed at the end of the looping construct. In addtion, the output file is also closed at the end of the programs execution.
Hopefully you enjoyed the podcast!


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2011-03-11 03:01:26 by pokey

Wow. This was really well presented. I usually gloss over at code. I just have a hard time following, but not so much this time. Your show was really good, and I really enjoyed it.

Comment #2 posted on 2011-03-15 13:07:40 by Doug

Thanks!

Pokey,
Yeah, I was concerned about doing a programming podcast in audio only, I'm glad it made sense!
Thanks for the nice comment!
Doug

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?