Python argparse not working corrrectly with path in Windows

Question:

The argparse library in python doesn’t work for the path, containing space, and ” (backslash) at the end.
The parser in argparse parses the backslash at the end of path to " (double quotation).

The code below is sample that has same problem.

import argparse


if __name__=="__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-w', '--weight', type=str)

    args = parser.parse_args()

    print(args.weight_path)

For example in the PowerShell,

PS > python sample.py -w ".test test"
.test test"

It does work for the path which doesn’t containing space.

PS > python sample.py -w ".testtest"
.test test
PS > python sample.py -w "testtest"
test test
PS > python sample.py -w "testtest"
test test

Is there any problem for using argparse with PowerShell?

I don’t even know how to search for this problem…

Asked By: Cynki

||

Answers:

You’re seeing what is a bug in Windows PowerShell, which has since been corrected in PowerShell (Core) 7+:

  • To PowerShell, based on the syntax of its own string literals, the verbatim value of string ".test test" is .test test, because has no special meaning in PowerShell (PowerShell’s escape character is `, the so-called backtick).

  • However, based on the most widely used conventions for parsing a process command line on Windows, this string must be placed as ".test test\" on the process command line constructed behind the scenes when calling an external program, given that processes are expected to parse " as an escaped " char.

    • While PowerShell (Core) 7+ now does do that behind the scenes, Windows PowerShell does not: it places ".test test", which causes Python to interpret the closing " as a verbatim " (while not complaining that a closing unescaped " – i.e. one with syntactic function – is then missing).

The workaround for Windows PowerShell is to manually double the char. before the closing ":

# Windows PowerShell only: double the  before the closing "
python sample.py -w ".test test\"

As an aside: While PowerShell (Core) 7+ has fixed this particular problem, there is still a long-standing problem with respect to intentionally embedded " characters in arguments passed to external programs – see this answer.

Answered By: mklement0
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.