[Python-ideas] Draft PEP on string interpolation

Akira Li 4kir4.1i at gmail.com
Sun Aug 23 11:27:15 CEST 2015


Nick Coghlan <ncoghlan at gmail.com> writes:

>     os.system(f"echo {filename}")
>     subprocess.call(f"echo {filename}")
>     subprocess.call(["echo", filename])
>
> Even in that simple case, the two unsafe approaches are much nicer to
> read, and as the command line gets more complex, the safe version gets
> harder and harder to read relative to the unsafe ones.

subprocess.call does not run the shell by default and therefore
subprocess.call(f"echo {filename}") will fail on POSIX (unless there is
an executable named echo<space>...).

If you meant shell=True then the right way is already the hard way:
pipes and redirections are more readable and less error-prone if written
using the shell syntax [1] (unless something like plumbum [2] is used)
and therefore people already might use the unsafe string formatting
without the corresponding shlex.quote() calls

[1]
http://stackoverflow.com/questions/295459/how-do-i-use-subprocess-popen-to-connect-multiple-processes-by-pipes
[2] https://pypi.python.org/pypi/plumbum



More information about the Python-ideas mailing list