Questions tagged [shlex]

Use this tag for questions about the python shlex module.

From python.docs:

The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for example, in run control files for Python applications) or for parsing quoted strings.

94 questions
7
votes
1 answer

most efficient way to parse this scripting language

I'm implementing an interpreter for a long-outdated text editor's scripting language, and I'm having some trouble getting a lexer to work properly. Here's an example of the problematic part of the language: T L /LOCATE ME/ C /LOCATE ME/CHANGED ME/ *…
Robbie Rosati
  • 1,205
  • 1
  • 9
  • 23
6
votes
1 answer

Wildcard not working in subprocess call using shlex

Language: Python v2.6.2 OS: AIX 5.3 I'm using Python to restore some files from a backup to a test system - all commands are called in the manner below, however some just plain don't want to work. #!/usr/bin/python import subprocess, shlex cmd =…
Sparc
  • 287
  • 1
  • 3
  • 7
6
votes
2 answers

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 "", line 1, in s = shlex.split(s) File…
parsecer
  • 4,758
  • 13
  • 71
  • 140
6
votes
0 answers

How can I deal with multi-character operators when lexing with shlex in Python?

I'm writing a language parser/interpreter, and I figured I could use the shlex module for generating tokens, but ran into an issue when working with multi-character operators, such as += or **. The shlex module will lex those as two separate…
Sohcahtoa82
  • 619
  • 3
  • 13
6
votes
1 answer

Python 3 backward compatability (shlex.quote vs pipes.quote)

One of my projects uses shlex.quote which is available since python 3.3. But that shlex.quote is the same as pipes.quote which is deprecated after moving to shlex. Now for compatibility I am using this code: def cmd_quote(string): import sys …
VP.
  • 15,509
  • 17
  • 91
  • 161
5
votes
1 answer

Parse input when dealing with file names

How can I parse the input when it is a list of paths? file_in = input("Insert paths: ") # foo.jpg "C:\Program Files\bar.jpg" print(file_in) # foo.jpg "C:\Program Files\bar.jpg" I'm looking for a clean way to get the input foo.jpg "C:\Program…
isar
  • 1,661
  • 1
  • 20
  • 39
4
votes
1 answer

How to quote paths on windows? (similar to shlex.quote)

I need to quote a path in a portable way, shlex.quote is converting slashes to / on windows, which causes the command to fail (using the default windows command prompt). Returning C:/Users/Me/Documents/Projects/Test Instead of…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
4
votes
1 answer

elisp equivalent of python shlex.split?

I need to parse a command line in elisp, something like: (shlex-split "command \"Some file with spaces\" someother\ quote") ;;That gives ("command" "Some file with spaces" "someother quote") How can I accomplish this in a simple way?
pygabriel
  • 9,840
  • 4
  • 41
  • 54
4
votes
1 answer

shlex preserving double quotes?

I'm using Popen with shlex for a yum command with the --exclude flag to pass a list of packages to be excluded. For some reason it seems shlex is not preserving the double quotes. Any pointers how do i go about this ? >>> import shlex >>> x =…
hmmm
  • 93
  • 2
  • 9
4
votes
1 answer

Using Python to cat a file over ssh to a remote bash script

I'm using subprocess.popen with shlex to call a remote bash script using ssh. This command works quite fine on bash itself. But as soon as I try to translate it to python and shlex with subprocess.popen it errs out. Remote bash…
Chris
  • 135
  • 2
  • 9
3
votes
2 answers

Split shell-like syntax in Haskell?

How can I split a string in shell-style syntax in Haskell? The equivalent in Python is shlex.split. >>> shlex.split('''/nosuchconf "/this doesn't exist either" "yep"''') ['/nosuchconf', "/this doesn't exist either", 'yep']
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
3
votes
0 answers

command for subprocess popen not working with shell=False

I am trying to run a subprocess with my own iptables rule for a subprocess using subprocess.open. The command works with shell=True. But when I remove the shell option, which it sets the shell option to false. The command doesn't work anymore. Here…
EzyHoo
  • 301
  • 2
  • 14
3
votes
1 answer

Preserve quotes when using shlex.split

How can one preserve the quotes around "value with spaces" when using shlex.split()? s = "SOME_VAR=\"value with spaces\" VAR2=value2" shlex.split(s) ['SOME_VAR=value with spaces', 'VAR2=value2'] Thank you
lubyou
  • 31
  • 1
3
votes
2 answers

shlex: Escaping quotes in Python 3

I want to split the ascii text 1 'K\^o, Suk\'e' which is printed as the Python string line = "1 'K\\^o, Suk\\'e'\n" into ['1', 'K\\^o, Suk\\'e'] shlex.split(line) doesn't work, giving a ValueError("No closing quotation"). I tried adding to…
user14717
  • 4,757
  • 2
  • 44
  • 68
3
votes
4 answers

Python split string by spaces except when in quotes, but keep the quotes

Am wanting to split the following string: Quantity [*,'EXTRA 05',*] With the desired results being: ["Quantity", "[*,'EXTRA 05',*]"] The closest I have found is using shlex.split, however this removes the internal quotes giving the following…
user3043991
  • 49
  • 1
  • 3