Advantages of subprocess over os.system

First of all, you are cutting out the middleman; subprocess.call by default avoids spawning a shell that examines your command, and directly spawns the requested process. This is important because, besides the efficiency side of the matter, you don’t have much control over the default shell behavior, and it actually typically works against you regarding … Read more

Python os.system without the output

Avoid os.system() by all means, and use subprocess instead: with open(os.devnull, ‘wb’) as devnull: subprocess.check_call([‘/etc/init.d/apache2’, ‘restart’], stdout=devnull, stderr=subprocess.STDOUT) This is the subprocess equivalent of the /etc/init.d/apache2 restart &> /dev/null. There is subprocess.DEVNULL on Python 3.3+: #!/usr/bin/env python3 from subprocess import DEVNULL, STDOUT, check_call check_call([‘/etc/init.d/apache2’, ‘restart’], stdout=DEVNULL, stderr=STDOUT)

Python: How to get stdout after running os.system? [duplicate]

If all you need is the stdout output, then take a look at subprocess.check_output(): import subprocess batcmd=”dir” result = subprocess.check_output(batcmd, shell=True) Because you were using os.system(), you’d have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell. If you need to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)