[Python-de] os.system und os.popen

Sven R. Kunze srkunze at mail.de
Do Mai 25 13:06:05 EDT 2017


On 25.05.2017 17:40, Hermann Riemann wrote:
> error=os.system("latex ... 2> /tmp/log")
>
> Hermann
>    der das so machen würde
>

Sieht durchaus einfacher aus, ist aber nicht ganz unsicher, wenn's um 
das Thema Quoting von Parametern geht.

*Hier auch noch zum Nachlesen:*
https://docs.python.org/3.6/library/subprocess.html

The subprocess module allows you to spawn new processes, connect to 
their input/output/error pipes, and obtain their return codes. This 
module *intends to replace* several older modules and functions:
os.system
os.spawn*

*Überspitzt ausgedrückt:* subprocess ist der neue heiße Scheiß und 
os.system/os.popen der Schnee von gestern. ;-)


Daher empfehle auch ich, wie Volker:

try:
     output = subprocess.check_output(.......)
except subprocess.CalledProcessError as exc:
     # hier geht's zur Fehlerbehandlung, falls latex streikt
     print(exc.output)
else:
     # alles gut
     print(output)

Das funktioniert sowohl unter Python 2 als auch Python 3. Viel Erfolg 
damit. :)

Grüße,
Sven


Mehr Informationen über die Mailingliste python-de