I'm using shlex.split
to tokenize arguments for a subprocess.Popen
call. However, when one of those args is a UNC path, things get 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 backslashes in the front are stripped down. I am working around this by adding the following two lines, b开发者_Go百科ut is there a better way?
if args[0].startswith('\\'):
args[0] = '\\' + args[0]
I don't know if this helps you:
>>> shlex.split(raw_args, posix=False)
['-path', '"\\\\server\\folder\\file.txt"', '-arg', 'SomeValue']
Try this:
raw_args = r'-path "\\\\server\\folder\\file.txt" -arg SomeValue'
Note r before the opening single quote.
精彩评论