Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making shlex.split respect UNC paths

Tags:

python

shlex

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, but is there a better way?

if args[0].startswith('\\'):
    args[0] = '\\' + args[0]
like image 460
Adam Lear Avatar asked Jan 28 '11 16:01

Adam Lear


1 Answers

I don't know if this helps you:

>>> shlex.split(raw_args, posix=False)
['-path', '"\\\\server\\folder\\file.txt"', '-arg', 'SomeValue']
like image 161
dusan Avatar answered Nov 10 '22 14:11

dusan