Creating shlex.split respects UNC paths

I use shlex.splittokenize arguments to call subprocess.Popen. However, when one of these arguments is a UNC loop, everything gets hairy:

import shlex

raw_args = '-path "\\\\server\\folder\\file.txt" -arg SomeValue'
args = shlex.split(raw_args)

print raw_args
print args

produces

-path "\\server\folder\file.txt" -arg SomeValue
['-path', '\\server\\folder\\file.txt', '-arg', 'SomeValue']

As you can see, the backslash in front is truncated. I am working on this by adding the following two lines, but is there a better way?

if args[0].startswith('\\'):
    args[0] = '\\' + args[0]
+5
source share
2 answers

I don't know if this will help you:

>>> shlex.split(raw_args, posix=False)
['-path', '"\\\\server\\folder\\file.txt"', '-arg', 'SomeValue']
+10
source

Try the following:

raw_args = r'-path "\\\\server\\folder\\file.txt" -arg SomeValue'

Pay attention to r before opening a single quote.

0
source

All Articles