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
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor