shlex.split with posix=false is not working (search for a file remotely)

2.2k Views Asked by At
gap = "dir c:\\PROGRA~2\\td\\conf\\ga.db3"
print gap
cmd = shlex.split('cmd "/c ' + gap+'"', posix=False)
print cmd
o = subprocess.call(cmd)
print o

The above script I am running on windows and it doesnt work at all. I just want to search a file "ga.db3" exists or not remotely. Please let me know.

The output is: File Not Found
1

There are 1 best solutions below

0
On

You must use posix=True to remove escaped quotes.

For difference between POSIX and non-POSIX read below the line or from the docs.


Parsing rules in non-POSIX mode

  • Quote characters are not recognized within words (Do"Not"Separate is parsed as the single word Do"Not"Separate)
  • Escape characters are not recognized
  • Enclosing characters in quotes preserve the literal value of all characters within the quotes;
  • Closing quotes separate words ("Do"Separate is parsed as "Do" and Separate)
  • If whitespace_split is False, any character not declared to be a word character, whitespace, or a quote will be returned as a single-character token. If it is True, shlex will only split words in whitespaces
  • EOF is signaled with an empty string ('')
  • It’s not possible to parse empty strings, even if quoted.

Parsing rules in POSIX mode

  • Quotes are stripped out, and do not separate words ("Do"Not"Separate" is parsed as the single word DoNotSeparate)
  • Non-quoted escape characters (e.g. '\') preserve the literal value of the next character that follows
  • Enclosing characters in quotes which are not part of escapedquotes (e.g. "'") preserve the literal value of all characters within the quotes
  • Enclosing characters in quotes which are part of escapedquotes (e.g. '"') preserves the literal value of all characters within the quotes, with the exception of the characters mentioned in escape. The escape characters retain its special meaning only when followed by the quote in use, or the escape character itself. Otherwise the escape character will be considered a normal character. EOF is signaled with a None value
  • Quoted empty strings ('') are allowed