There is such code:

import argparse parser = argparse.ArgumentParser(description='test') parser.add_argument('-p',help='Path to file') args = parser.parse_args() fp = args.p print fp 

If you transfer there -p C: \ Program Files (x86) \. Will write an unrecognized argument. How to fix it?

  • The question is not related to argparse. Try sys.argv to be sure. The question is relevant as the arguments from cmd are passed to python. For example, in cmd.exe you will have to escape special characters such as ^ or spaces ( ^ used in cmd to escape other characters, spaces to separate arguments from each other) - jfs

1 answer 1

Spaces in the path are perceived as other arguments, so if there are spaces, you need to wrap the value in quotes:

 -p "C:\Program Files (x86)\" 
  • It is worth mentioning that this line is not in Python, but most likely in the console running cmd.exe - jfs