#1 Le 21/05/2020, à 14:57
- alex2423
Comment executer la commande Shell SED sur Python avec subprocess?
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
Hors ligne
#2 Le 21/05/2020, à 15:29
- pingouinux
Re : Comment executer la commande Shell SED sur Python avec subprocess?
Bonjour,
Ce serait mieux si tu montrais un script qu'on puisse tester en lançant directement l'exécution.
Il y a un ' en trop dans la ligne suivante :
s = objectShellBash("ls -l | grep toto'")
Essaye comme ceci pour l'autre commande :
s = objectShellBash("ls -l | sed -nr 's/.*\s([a-z]+).py$/\\1/p'")
Hors ligne
#3 Le 21/05/2020, à 15:50
- alex2423
Re : Comment executer la commande Shell SED sur Python avec subprocess?
Oui j'ai tapé la commande avec le grep toto sans retester, oups ..
Bon en effet je viens de m'apercevoir que ce qui posait problème était le le l’antislash de "\1" dans le sed.
J'ai essayé comme toi, en doublant l'antislash "\\", mais même problème. Le "\" n'était toujours pas échapé
Et je viens de trouver comment utiliser l'antislash sans que Python l'interprète comme un caractère spécial. Il faut préfixer sa chaine de caractère avec R
https://docs.python.org/2.0/ref/strings.html
When an `r' or `R' prefix is present, backslashes are still used to quote the following character, but all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase `n'. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote;
[...]
Avec R :
s = objectShellBash(R"ls -l | sed -nr 's/.*\s([a-z]+).py$/\1/p'")
Voici le résultat, plus de problème. On retrouve par exemple mes 2 fichiers helloworld.py et sed.py
b'helloworld\nsed\n'
Dommage qu'il rajoute le retour chariot '\n' automatiquement
Dernière modification par alex2423 (Le 21/05/2020, à 15:51)
Hors ligne
#4 Le 21/05/2020, à 16:40
- pingouinux
Re : Comment executer la commande Shell SED sur Python avec subprocess?
Si tu ne veux pas recevoir le \n final :
self.output = ps.communicate()[0][:-1]
Hors ligne
#5 Le 21/05/2020, à 16:46
- Watael
Re : Comment executer la commande Shell SED sur Python avec subprocess?
exécuter une commande shell par python, c'est faire tirer une Ferrari par des bœufs.
et puis faire un sed sur ls
Connected \o/
Welcome to sHell. · eval is evil.
Hors ligne