[Tutor] Read from large text file, parse, find string, print string + line number to second text file.
Dave Angel
davea at davea.name
Fri Feb 1 23:22:49 CET 2013
On 02/01/2013 04:24 PM, Scurvy Scott wrote:
> One last question on this topic..
>
> I'd like to call the files and the string form the command line like
>
> Python whatever.py STRINGTOSEARCH NEWFILE FILETOOPEN
>
> My understanding is that it would be accomplished as such
>
> import sys
>
> myString = sys.argv[1]
> filetoopen = sys.argv[2]
> newfile = sys.argv[3]
Notice that you have 2 and 3 reversed from your description above.
Neither one is wrong, but you have to pick one of them.
If it were my utility, I'd not specify any outfile on the commandline,
but use redirection as is normal in Linux/Unix/Windows. In other words,
just use print as your output, and don't have an argument for the output
file.
>
>
> ETC ETC CODE HERE
>
If it were my code, I'd be first checking the number of arguments (
len(sys.argv) ). If the number isn't exactly right, print a usage
string and exit the program.
If your commandline gets any more complex, consider using the argparse
module.
http://docs.python.org/2/library/argparse.html?highlight=argparse#argparse
--
DaveA
More information about the Tutor
mailing list