python - Comment exécuter un programme ou appeler une commande système

Mots clés : pythonshellterminalsubprocesscommandpython

meilleur 5 Réponses python - Comment exécuter un programme ou appeler une commande système

vote vote

97

import subprocess subprocess.run(["ls", "-l"]) 
subprocess.call(["ls", "-l"]) 
vote vote

84

os.system("some_command < input_file | another_command > output_file")   
print(os.popen("ls -l").read()) 
print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read() 
print os.popen("echo Hello World").read() 
return_code = subprocess.call("echo Hello World", shell=True) 
print subprocess.Popen("echo %s " % user_input, stdout=PIPE).stdout.read() 
vote vote

79

import subprocess  p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in p.stdout.readlines():     print line, retval = p.wait() 
vote vote

69

import subprocess import sys  # Some code here  pid = subprocess.Popen([sys.executable, "longtask.py"]) # Call subprocess  # Some more code here 
DETACHED_PROCESS = 0x00000008  pid = subprocess.Popen([sys.executable, "longtask.py"],                        creationflags=DETACHED_PROCESS).pid 
pid = subprocess.Popen([sys.executable, "longtask.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) 
vote vote

52

import os os.system("your command") 

Questions similaires