[Tutor] Read from large text file, parse, find string, print string + line number to second text file.
etanes.rm at gmail.com
Sat Feb 2 05:31:49 CET 2013
> Best practice is to check if your program is being run as a script before
> doing anything. That way you can still import the module for testing or
> def main(mystring, infile, outfile):
> # do stuff here
> if __name__ == '__main__':
> # Running as a script.
> import sys
> mystring = sys.argv
> infile = sys.argv
> outfile = sys.argv
> main(mystring, infile, outfile)
> Best practice for scripts (not just Python scripts, but *any* script) is to
> provide help when asked. Insert this after the "import sys" line, before you
> start processing:
> if '-h' in sys.argv or '--help' in sys.argv:
> If your argument processing is more complicated that above, you should use
> one of the three argument parsing modules that Python provides:
> http://docs.python.org/2/library/optparse.html (deprecated -- do not use
> this for new code)
> getopt is (in my opinion) the simplest to get started, but the weakest.
> There are also third-party argument parsers that you could use. Here's one
> which I have never used but am intrigued by:
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
thanks a lot for showing me the if __name__ = main part
I've often wondered how it was used and it didn't make sense until I
saw it in my own code if that makes any sense.
Also appreciate the help on the "instructional" side of things.
One question related to the instruction aspect- does this make sense to you?
If len(sys.argv) == 0:
print "usage: etc etc etc"
Nick, Dave, and Steve, again, you guys are awesome. Thanks for all your help.
More information about the Tutor