Вот функция системного вызова, которую я собрал несколько лет назад и использовал в различных проектах.Если вам вообще не нужен вывод команды, вы можете просто сказать out = syscmd(command)
, а затем ничего не делать с out
.
Протестировано и работает в Python 2.7.12 и 3.5.2.
def syscmd(cmd, encoding=''):
"""
Runs a command on the system, waits for the command to finish, and then
returns the text output of the command. If the command produces no text
output, the command's return code will be returned instead.
"""
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
close_fds=True)
p.wait()
output = p.stdout.read()
if len(output) > 1:
if encoding: return output.decode(encoding)
else: return output
return p.returncode