shlex

Run Perl code (with output to file) from Python

ⅰ亾dé卋堺 提交于 2019-11-30 20:49:51
问题 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 try to do this in Python it doesn't work. This the code: import shlex import subprocess args_str = "perl myCode.pl > results.txt" args = shlex.split(args_str) subprocess.call(args) Despite the > results.txt it does not output to that file but it

python, windows : parsing command lines with shlex

坚强是说给别人听的谎言 提交于 2019-11-30 04:49:36
问题 When you have to split a command-line, for example to call Popen , the best practice seems to be subprocess.Popen(shlex.split(cmd), ... but RTFM The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell ... So, what's the correct way on win32? And what about quote parsing and POSIX vs non-POSIX mode? 回答1: There is no valid command-line splitting function so far in the Python stdlib for Windows/multi-platform so far. (Mar 2016) subprocess So

Python: Split a string, respect and preserve quotes [duplicate]

假如想象 提交于 2019-11-29 02:47:52
问题 This question already has answers here : Split a string by spaces — preserving quoted substrings — in Python (15 answers) Closed 3 years ago . Using python, I want to split the following string: a=foo, b=bar, c="foo, bar", d=false, e="false" This should result in the following list: ['a=foo', 'b=bar', 'c="foo, bar"', 'd=false', 'e="false'"'] When using shlex in posix-mode and splitting with ", ", the argument for c gets treated correctly. However, it removes the quotes. I need them because

Wildcard not working in subprocess call using shlex

≯℡__Kan透↙ 提交于 2019-11-28 11:29:55
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 = 'sudo rm -rf /work/TEST/*' arg = shlex.split(cmd) # This does not work p = subprocess.Popen(arg) # This, however, works just fine p = subprocess.Popen(cmd, shell=True) If I remove the *'s from the commands they work fine (well, they work as they should without the wildcards, which is unfortauntely not what I want). I really do not want to use shell