0
def check_file(user_name,default_name):
    while True:
        try:
            #### check user name matches the default name
            if ('%s'%(user_name)) == '%s'%(default_name):
                print("file matches")
                break
        except:
            print("wrong file.")
            continue
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-k1',type=argparse.FileType('r'),default='k1file.txt',required=True,
                        help='file input')

    parser.add_argument('-k2',type=argparse.FileType('r'),default='k2file.txt',required=True,
                        help='file input')    
    args = parser.parse_args()
    check_file(args.k1,args.get_default('k1'))
    check_file(args.k2,args.get_default('k2'))

Present output:

AttributeError: 'Namespace' object has no attribute 'k1'
6
  • 1
    When debugging include a print(args), so you see what the parser has done, without any further assumptions or processing. Off hand it looks like it should have a k1 attribute, but I'd like to see the print to be sure. What's that args.get_default('k1') supposed to do? It doesn't look right. Commented Oct 14, 2021 at 21:26
  • @hpaulj I will try your suggestion for debugging. I wanted to compare the user input file with the default file? are the names the same? are both the same type? Commented Oct 14, 2021 at 21:30
  • Anyways, as the answer says, the args namespace object does not have access to the default. That's an attribute of the Action object created by add_argument. Why are you expecting the user to enter the same thing as the default? Just to make them jump-through-some-hoops? Why required is True? Commented Oct 14, 2021 at 21:39
  • Another thing to watch out for. The default is a string. But with that type, the value in args will be an opened file. They won't be equal. That's yet another reason to print the args, to check on the value as well as name. When new to a module, be careful about your assumptions. Commented Oct 14, 2021 at 21:44
  • 1
    Omit the type and just accept a filename string. You can open it later in a with context if it is right. Commented Oct 15, 2021 at 0:32

1 Answer 1

1

The add_argument method returns the Action that was just added. Which has details about the argument definition - including the default value.

import argparse

parser = argparse.ArgumentParser()

arg_action = parser.add_argument('-f', default='file')

print(arg_action.default)

Prints:

file

And you should be able to use that to compare the actual arguments with the default values.

Sign up to request clarification or add additional context in comments.

2 Comments

but how do we compare the input with default? I am surprised why my code is throwing error.
Nothing wrong there. args.k1 should give you the actual value. Check for typos in your code. I couldn't reproduce your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.