New python user seeks comments

David Steuber trashcan at david-steuber.com
Wed Apr 21 00:33:10 EDT 1999


Er, uh, hmm.

I wrote my first Python program today.  It took longer than I
expected.  Who would have guessed that you couldn't give a file object 
the name 'in'?  Or at least that was one of the weirder obstacles.
Anyway, I am not at all used to thinking in Python.  I was wondering
if anyone would care to offer comments on my Python script.  It is
quite small and is responsible for my sig.  Feel free to scarf it.

-------------- next part --------------
#! /usr/bin/env python

# Tue Apr 20 17:31

# This is my first python program.  It is intended to generate signature
# files for mail and news using the fortune program.

# The first step is to open a template file that contains the constant
# portion of the signature.  For flexability, I would like to get
# the file name from the command line.  So really, the first step is
# to read the command line.

# imports section
import sys
import os
import commands
import time

# Test the number of arguments to see that it is the required number.
if len (sys.argv) != 3:
    print 'usage: siggen.python template pipe'
    sys.exit(0)

# The first argument is the input file, the second argument is the
# output file which should be a named pipe.
template = sys.argv[1]
npipe = sys.argv[2]

if not os.path.isfile(template):
    print template + ' is not a file!'
    sys.exit(0)

if not os.path.exists(npipe):
    os.mkfifo(npipe)

# Time to get into it.  Go into an infinite loop where we open the
# template file, copy it line by line to the pipe, then execute the
# fortune program and send it out the pipe
while 1:
    fin = open(template, 'r', 1)
    fout = open(npipe, 'w', 0)
    fout.writelines(fin.readlines())
    fin.close()
    fout.writelines([commands.getoutput('/usr/bin/fortune -a'),'\n'])
    fout.close()
    time.sleep(1)
-------------- next part --------------

-- 
David Steuber
http://www.david-steuber.com

s/trashcan/david/ to reply by mail | while you're at it, you might also
If you don't, I won't see it.      | want to track down and kill bulk
                                   | mailers, aka spammers.

Vote for ME -- I'm well-tapered, half-cocked, ill-conceived and
TAX-DEFERRED!


More information about the Python-list mailing list