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
3
votes
4 answers

Why do I need 4 backslashes in a Python path?

When I'm using Python 3 to launch a program via subprocess.call(), why do I need 4 backslashes in paths? This is my code: cmd = 'C:\\\\Windows\\\\System32\\\\cmd.exe' cmd = shlex.split(cmd) subprocess.call(cmd) When I examine the command line of…
kiri
  • 2,522
  • 4
  • 26
  • 44
3
votes
2 answers

Shlex Split Equivalent for Node.js

How would I do the following in Node.js? I realize there's probably no builtin feature or written module for this, so how might I implement this? >>> import shlex >>> shlex.split("-a arga -b \"argument b\" arg1 arg2") ['-a', 'arga', '-b', 'argument…
skeggse
  • 6,103
  • 11
  • 57
  • 81
2
votes
0 answers

In which cases shlex.quote() is used?

I stumbled across shlex.quote(). I have read explanations about what shlex.quote() is, but I am wondering when to use it and when not. I see that I should use it when I am using Python as a subshell. For example, using os.system() or…
yasa
  • 126
  • 7
2
votes
1 answer

What is the difference between distutils.util.split_quoted and shlex.split

The python standard library provides distutils.util.split_quoted and shlex.split. Is there any situation in which distutils.util.split_quoted(s) gives a different result to shlex.split(s)?
Eric
  • 95,302
  • 53
  • 242
  • 374
2
votes
1 answer

Python Shlex splitting with brackets

I have a need to split a series of strings into 3 component parts denoted by spaces. These strings sometimes contain sublists, but always as the last component of the string. I was previously using Shlex to achieve this with great success, but I'm…
danspants
  • 3,099
  • 3
  • 27
  • 33
2
votes
0 answers

Bash: Tokenize string using shell rules without eval'ing it?

I'm writing a wrapper script. The original program's arguments are in a separate file, args. The script needs to split contents of args using shell parameter rules and then run the program. A partial solution (set + eval) was offered in Splitting a…
nvamelichev
  • 398
  • 3
  • 13
2
votes
1 answer

Run Perl code (with output to file) from Python

I'm trying to run a Perl script from Python. I know that if run the Perl script in terminal and I want the output of the Perl script to be written a file I need to add > results.txt after perl myCode.pl. This works fine in the terminal, but when I…
jksnw
  • 648
  • 1
  • 7
  • 19
2
votes
1 answer

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

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…
user2511126
  • 620
  • 1
  • 14
  • 31
2
votes
2 answers

Python shlex.split() cannot retain single quotes

I have the following text: 'sudo -S java -cp spinn3r-client-3.4.06.jar com.spinn3r.api.Main --vendor=test --remote-filter=\'(and (eq source:publisher_type " WEBLOG") (eq dc_lang:English) \'' Now I need to split this to run using the subprocess…
Amitash
  • 1,029
  • 6
  • 16
  • 26
2
votes
1 answer

Python shlex - Split

I would like to split thanks to shlex this kind of string: str = 'This doesn''t work' 54e+5 15 .FALSE. 'Another example of "test"' Result expected: This doesn''t work 54e+5 15 .FALSE. Another example of "test" My main issue is that the syntax…
thomas
  • 51
  • 1
  • 5
1
vote
1 answer

Python: Parsing data containing both types of quotation as well as special characters

Hi All I am working on a project where I need to parse some data containing both " and ' quotation marks as well as special characters. While the data is confidential and therefore cannot be posted on here the text below replicates the…
Pioneer_11
  • 670
  • 4
  • 19
1
vote
2 answers

Python3: shell quoting less complicated than shlex.quote() output?

In python3.8, I have this code: import shlex item = "ABC'DEF" quoteditem = shlex.quote(item) print(quoteditem) This is the output: 'ABC'"'"'DEF' It's difficult to discern the double and single quotes here on this web page, so this is a…
HippoMan
  • 2,119
  • 2
  • 25
  • 48
1
vote
3 answers

Taming shlex.split() behaviour

There are other questions on SO that get close to answering mine, but I have a very specific use case that I have trouble solving. Consider this: from asyncio import create_subprocess_exec, run async def main(): command = r'program.exe…
Grismar
  • 27,561
  • 4
  • 31
  • 54
1
vote
1 answer

Split arguments like shlex.split but convert environment variables

I want to split a string into command-line arguments, exactly like shlex.split does. However, shlex doesn't seem to convert environment variables (for example $USER), and the output makes it impossible to know whether the environment variable was…
NeatNit
  • 526
  • 1
  • 4
  • 14
1
vote
1 answer

Preserve "\t", "\n", "\r" when spliting a string using shlex in python

I am trying to split a string using shlex but I want to preserve(literally) "\t", "\n", "\r". Example: text = "a=\t\n\r\example c=d" sp_text = shlex.split(text) print(sp_text) ['a=', 'example', 'c=d'] But I want to print this: ['a=\t\n\r\example',…