This simple code:
s = "it's a nice day..."
s = shlex.split(s)
Would result in a ValueError: No closing quotation
error:
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
s = shlex.split(s)
File "C:\Python\Python35-32\lib\shlex.py", line 273, in split
return list(lex)
File "C:\Python\Python35-32\lib\shlex.py", line 263, in __next__
token = self.get_token()
File "C:\Python\Python35-32\lib\shlex.py", line 90, in get_token
raw = self.read_token()
File "C:\Python\Python35-32\lib\shlex.py", line 166, in read_token
raise ValueError("No closing quotation")
ValueError: No closing quotation
I assume the '
is at fault. How do I deal with it? I read this line from a file, so I can't just type \
before each quote or something.
You should use shlex.quote(s)
to safely escape the read input prior to split. If you check the docs at the link, quote
is compatible with split.
import shlex
s = "it's a nice day..."
sq = shlex.quote(s)
print(sq) # '\'it\'"\'"\'s a nice day...\''
shlex.split(sq) # ["it's a nice day..."]
The use of the quote
function will also protect you from 'injection attacts' which you should be aware of if you are going to accept un-trusted (not your own) input. See the docs for an example of a rm -rf ~
bomb!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With