[Tutor] How can I modify this simple script for argparse?
Kwpolska
kwpolska at gmail.com
Sat Oct 13 19:13:22 CEST 2012
On Sat, Oct 13, 2012 at 7:03 PM, Santosh Kumar <sntshkmr60 at gmail.com> 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?
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
The documentation may be hard to understand, but the tutorial isn’t:
<http://docs.python.org/howto/argparse.html>
Anyways, here you go:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename', action='store')
args = parser.parse_args()
foo = "This line was written by a Python script."
with open(args.filename, 'a') as file:
file.write(foo)
Note that this is the most basic version. Also, I had problems with
running it as a standalone file, but it worked fine in the interactive
interpreter.
--
Kwpolska <http://kwpolska.tk>
stop html mail | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16
More information about the Tutor
mailing list