On Thu, Aug 26, 2021, at 08:50, Evan Greenup via Python-ideas wrote:
Currently, when what to execute external command, either os.system() or subprocess functions should be invoked.
However, it is not so convenient, it would be nice to use "``" symbol to brace the shell command inside.
like:
stdout_result_str = `cat file.txt | sort | grep hello`
I don't think this is an important enough feature to be worth using up one of the few free ascii characters available for new syntax. However, it might be useful to have a helper function to build pipelines without the shell, so you could take the list [['cat', 'file.txt'], ['sort'], ['grep', 'hello']] and get this result. Like, currently, to set up this pipeline (putting aside the useless use of cat), you have to do the following: p1 = subprocess.Popen(['cat', 'file.txt'], stdout=PIPE) p2 = subprocess.Popen(['sort'], stdout=PIPE, stdin=p1.stdout) result = subprocess.check_output(['grep', 'hello'], stdin=p2.stdout) [there are nuances about closing the intermediate pipes in the parent process that I've left out of this simple example] something like subprocess.check_output_pipelined(['cat', 'file.txt'], ['sort'], ['grep', 'hello']) might be useful.
Its is enhanced feature which combine both subprocess and os.system
To do this *with* a shell, you can use os.popen and read the resulting file stream. But there are good reasons, I think, to want to be able to do this without a shell.