Bonjour,
Je me suis créé une petite classe à partir trouvaille pour lancer une commande shell à partir de Python (incluant les commandes pipe)
class objectShellBash:
def __init__(self, cmd):
self.cmd = cmd
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
self.output = ps.communicate()[0]
def get_output(self):
return self.output
Juste pour donner un exemple, un pipe avec un grep fonctionne parfaitement.
s = objectShellBash("ls -l | grep toto'")
Mais par exemple la commande ne fonctionne plus.
s = objectShellBash("ls -l | sed -nr 's/.*\s([a-z]+).py$/\1/p'")
J'obtiens comme retour : b'\x01\n\x01\n'
Et en executant les commandes depuis le shell :
xbionic@bionic:~/PycharmProjects/firstProject$ ls -l
total 56
-rw-rw-r-- 1 xbionic xbionic 0 mai 21 12:21 fichier
-rw-rw-r-- 1 xbionic xbionic 2728 mai 21 15:49 helloworld.py
drwxrwxr-x 2 xbionic xbionic 4096 mai 21 15:35 __pycache__
-rw-rw-r-- 1 xbionic xbionic 42436 oct. 26 2014 sed.py
drwxrwxr-x 5 xbionic xbionic 4096 mai 21 11:58 venv
xbionic@bionic:~/PycharmProjects/firstProject$ ls -l | sed -nr 's/.*\s([a-z]+).py$/\1/p'
helloworld
sed