[Tutor] Read from large text file, parse, find string, print string + line number to second text file.
Scurvy Scott
etanes.rm at gmail.com
Sat Feb 2 05:57:41 CET 2013
And just for the records sake, this is what I've gotten and you guys
should see obviously that you helped a lot and I learned a thing or
two so I won't have to ask the same silly questions next time:
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."
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:
print (help_text)
sys.exit()
myString = sys.argv[1]
infile = sys.argv[2]
outfile = sys.argv[3]
main(mystring, infile, outfile)
Look right to you? Looks okay to me, except maybe the three ORs in the
information line, is there a more pythonic way to accomplish that
task?
Scott
On Fri, Feb 1, 2013 at 8:31 PM, Scurvy Scott <etanes.rm at gmail.com> wrote:
>> 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
>> similar:
>>
>>
>> def main(mystring, infile, outfile):
>> # do stuff here
>>
>>
>> if __name__ == '__main__':
>> # Running as a script.
>> import sys
>> mystring = sys.argv[1]
>> infile = sys.argv[2]
>> outfile = sys.argv[3]
>> 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:
>> print(help_text)
>> sys.exit()
>>
>>
>>
>> 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/getopt.html
>> http://docs.python.org/2/library/optparse.html (deprecated -- do not use
>> this for new code)
>> http://docs.python.org/2/library/argparse.html
>>
>>
>> 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:
>>
>> http://docopt.org/
>>
>>
>>
>> --
>> Steven
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> Steve-
> 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.
>
> Scott
More information about the Tutor
mailing list