[Tutor] How can I modify this simple script for argparse?

Steven D'Aprano steve at pearwood.info
Sat Oct 13 19:14:37 CEST 2012


On 14/10/12 04:03, Santosh Kumar wrote:
> Here is a sample script without argparse implementation:
>
> from sys import argv
> script, filename = argv
>
> foo = "This line was written by a Python script."
>
> with open(filename, 'a') as file:
>      file.write(foo)
>
>
> I find argparse hard. Just give me a startup. How can I make a asgparse version?


import argparse

parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()

spam = "This line was written by a Python script."
with open(args.filename, 'a') as file:
     file.write(spam)




See also: http://docs.python.org/howto/argparse.html


-- 
Steven


More information about the Tutor mailing list