argparse and default for FileType

P

Paolo Elvati

Hi,

I noticed a "strange" behavior of argparse.
When running a simple code like the following:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-o",
default = 'fake',
dest = 'OutputFile',
type = argparse.FileType('w')
)
args = parser.parse_args()

I noticed that the default file (fake) is created every time I run the
code, even when I explicitly set the -o flag, in which case it will
produce both files.
My goal instead is to erite the default file ONLY if the flag is not specified.
For the moment, I solved it simply by removing the "default=fake" and
adding the "required=True" keyword, but I was wondering what is the
correct way of doing it (or if it is simply a bug).

Thank you,

Paolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top