On 02/02/13 04:57, Scurvy Scott wrote:

It may just be an email thing but...

def main(mystring, infile, outfile):
     with open('infile', 'r') as inF:
                for index, line in enumerate(inF):
                        if myString in line:
                                newfile.write("string %s found on line #%d" 
(line, index))
print "complete."

The print should be inside the function not outside.

And main is probably not the best name. You could call it
printFoundString or somesuch...

'main' is usually used to collect all the program driver code that you currently have under the if name... test. The stuff you wouldn't ever use if importing as a module. Your way works fine too, a main is not obligatory. :-)

if __name__ == '__main__':
    import sys
    newfile = open('outfile', 'w')
    help_text = "usage: python scanfile.py STRINGTOSEARCH
IMPORTFILENAME OUTPUTFILENAME"
    if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) == 0:

The last test should be == 1 since the program name will always be there. But in fact you need the string and file args too so it
should really be:

len(sys.argv) < 4

anything less than 4 args and your code breaks...

    myString = sys.argv[1]
    infile = sys.argv[2]
    outfile = sys.argv[3]

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to