argparse and default for FileType

Discussion in 'Python' started by Paolo Elvati, Apr 8, 2011.

  1. Paolo Elvati

    Paolo Elvati Guest

    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
    Paolo Elvati, Apr 8, 2011
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF1bA==?=

    Introduce new filetype with httphandler

    =?Utf-8?B?UGF1bA==?=, Jun 15, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    1,270
    =?Utf-8?B?UGF1bA==?=
    Jun 15, 2005
  2. Edwin Knoppert
    Replies:
    0
    Views:
    368
    Edwin Knoppert
    Dec 29, 2005
  3. Gerry
    Replies:
    0
    Views:
    322
    Gerry
    Feb 1, 2008
  4. Replies:
    0
    Views:
    220
  5. Replies:
    0
    Views:
    156
Loading...

Share This Page