开发者

Making shlex.split respect UNC paths

开发者 https://www.devze.com 2023-02-07 13:01 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号