Created
January 18, 2016 19:40
-
-
Save diegorocha/6d30298a5823ad3942f4 to your computer and use it in GitHub Desktop.
Exemplo de interação com outros processos usando o modulo subprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import subprocess | |
#Obtem o RETURN CODE de um programa, no caso o pwd | |
value = subprocess.call('pwd') | |
print value | |
#Obtem a saída normal (stdout) de um programa, no caso o pwd | |
value = subprocess.check_output('pwd') | |
print value | |
#Interage com o programa, enviando stdin (teclado) e obtendo stdout e stderr | |
p = subprocess.Popen(['python', 'process_with_stdin.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
output, err = p.communicate(b"foo\n") #Envia 'foo' como entrada do usuário | |
print output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
valor = raw_input('Insira um valor: ') | |
print('Você digitou: %s' % valor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment