Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python shlex No closing quotations error - how to deal with?

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.

like image 933
parsecer Avatar asked Mar 03 '23 14:03

parsecer


1 Answers

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!

like image 189
mgrollins Avatar answered Mar 26 '23 14:03

mgrollins