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
0
votes
1 answer

shlex.split() : How to keep quotes around sub-strings and not split by in-sub-string whitespace?

I want the input string "add [7,8,9+5,'io open'] 7&4 67" to be split like ['add', "[7,8,9+5,'io open']", '7&4', '67'], i.e, within the line, strings must remain within quotes and musn't be split at all , and otherwise whitespace based splitting is…
user426
  • 213
  • 2
  • 9
0
votes
0 answers

How to deal with multi-character commenter when lexing with shlex in Python?

I want to parse a string with some inline comment blocks like /* comments */, and I want to ignore those comments. For example, >>> import shlex >>> string = "foo /bar /* this is a test */" >>> lex = shlex.shlex(string) >>> list(lex) ['foo', 'bar',…
zljt3216
  • 53
  • 1
  • 5
0
votes
1 answer

Python subprocess.Popen giving error with unix 'find' command

I am trying to get the largest 20 files in a directory that are older than 60 days by passing the unix 'find' command to Python's subprocess.Popen. This is what I have tried: # Get largest files older than 60 days cmd_string = 'find {0} -type f…
daragh
  • 487
  • 9
  • 18
0
votes
1 answer

Python shlex split option that contains RegEx

Lets say I want to give this command ./maryam -e crawl_pages -d domain.tld -r "a href=\".*" and split it. When I run >>>line = './maryam -e crawl_pages -d domain.tld -r "a href=\".*"' >>>shlex.split(line) I get the following error Traceback (most…
z3y50n
  • 51
  • 1
  • 8
0
votes
1 answer

wget missing url with shlex and subprocess

I'm struggling to understand why this fails with a wget: missing URL error: import shlex import subprocess copy_command = "wget -O - 'http://example.com/somepath/somefile.txt?someparam=test' | sshpass -p pass ssh user@localhost -p 2222 \"cat - >…
L42
  • 3,052
  • 4
  • 28
  • 49
0
votes
1 answer

How to use a specific stop-character for shlex.split?

How to tell to shlex that if the character ; is found, then, don't split anything anymore? Example: shlex.split("""hello "column number 2" foo ; bar baz""") should give ["hello", "column number 2", "foo", "; bar baz"] instead of ["hello",…
Basj
  • 41,386
  • 99
  • 383
  • 673
0
votes
1 answer

How do I reliably use Fabric's cd() context manager

I'm trying to run a find command in a directory on a remote system. Fabric changes directory sometimes but sometimes it fails, depending on whether the path contains parentheses or spaces and whether I use shlex.quote() or not. What is the correct…
gazzar
  • 91
  • 1
  • 5
0
votes
0 answers

Error when trying to create a file in a path containing parentheses with Python

Can anyone spot why this is failing? import pathlib from shlex import quote path = 'testdir(abc)/subdir(123)' filename = 'test' content = \ """hello world """ pathlib.Path(path).mkdir(mode=0o770, parents=True, exist_ok=True) md5_filename =…
gazzar
  • 91
  • 1
  • 5
0
votes
1 answer

Shlex does not pass multiple consecutive commands properly to subprocess when separated by semicolon

I have two folders. I have created temp files as below $ touch -t 1604031305 files/tmpfile files2/tmpfile && tree . . ├── files │   └── tmpfile ├── files2 │   └── tmpfile └── test.py 2 directories, 3 files Now I can execute the following find…
0
votes
2 answers

How to execute a curl command in Python and get the response and pass it through other code

I have a curl command. I want to execute in in Python and fetch the response to pass it through other code. curl https://api.box.com/oauth2/token -d 'grant_type=refresh_token' -d…
Debasis
  • 83
  • 9
0
votes
1 answer

Prevent shlex from splitting with colon (:)

I'm having trouble dealing with colons (:) in shlex. I need the following behaviour: Sample input text = 'hello:world ("my name is Max")' s = shlex.shlex(instream=text, punctuation_chars=True) s.get_token() s.get_token() ... Desired…
MrMCMax
  • 3
  • 1
0
votes
0 answers

Python shlex posix usage dilemma

I came to this dilemma while working on improving my semi-automated library management for an ECAD software (namely KiCad), what is below is only an example which I hope represents the issue I ran into. The library file contains multiple lines and…
eeintech
  • 111
  • 5
0
votes
0 answers

How can i launch youtube fullscreen from terminal

i am trying to get running video via terminal call to get fullscreen and good resolution, but i don't know how to set arguments. i am on linux, i do this on terminal: firefox -new-tab https://www.youtube.com/watch?v=spUNpyF58BY but: 1-it's not…
laticoda
  • 100
  • 14
0
votes
1 answer

How to split a multi word string but escaping split at certain word combinations?

Say I have a string as: mystr = "my name is some good name" # I want to split at white space except for the part "name is" expectedoutput = ["my", "name is", "some", "good", "name"] How can I do it with and without shlex? The way I was trying to…
everestial007
  • 6,665
  • 7
  • 32
  • 72
0
votes
1 answer

How to retrive output from python subprocess

I'm using the Python subprocess function, because I want to read the output of the command in real time stream. But I would like that after the end of process, everything what was written as a stream output was completely returned to object. import…
criss
  • 43
  • 1
  • 3