2016-06-27 11 views
0

Ich versuche, den strings Befehl in einem Python-Skript auszuführen. Ich hatte es für eine Weile arbeiten, aber auf mysteriöse Weise gibt es nichts zurück, wenn ich subprocess.Popen mehr benutze.Python 2.7 Subprozess gibt nichts zurück, genaue Befehl funktioniert in der Konsole

Genannt von Klemme

strings /path/to/.bashrc 
# You may uncomment the following lines if you want `ls' to be colorized: 
# export LS_OPTIONS='--color=auto' 
# eval `dircolors` 
# alias ls='ls $LS_OPTIONS' 
# alias ll='ls $LS_OPTIONS -l' 
# alias l='ls $LS_OPTIONS -lA' 
# Some more alias to avoid making mistakes: 
# alias rm='rm -i' 
# alias cp='cp -i' 
# alias mv='mv -i' 
shopt -u -o history 

In Python-Shell:

import subprocess 
proc = subprocess.Popen(['strings', '/path/to/.bashrc'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 
out, err = proc.communicate() 

Der zweite kehrt nichts. Warum?

+0

Warum Sie Shell verwenden = True? –

Antwort

2

Vom subprocess documentation:

Das Shell-Argument (die Standardeinstellung False) gibt an, ob die Schale als das Programm zu verwenden, auszuführen. Wenn die Shell True ist, wird empfohlen, Argumente als String und nicht als Sequenz zu übergeben.

Versuchen Sie es mit:

proc = subprocess.Popen("strings /path/to/file", ...) 
+0

Was wäre das richtige Format für die Eingabe? – Ares

+0

Ich habe die Antwort entsprechend bearbeitet. – ynimous

+0

Wenn ich das auf eine bekannte Textdatei (output.txt, bestätigt über Terminal) laufe, bekomme ich '' [Errorno 2] keine solche Datei oder Verzeichnis "' – Ares