argparse and default for FileType

Paolo Elvati elvati at umich.edu
Fri Apr 8 14:11:30 EDT 2011


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



More information about the Python-list mailing list