2016-07-12 2 views
4

Ich möchte die Ausgabe von Python auf beide Konsole und eine Textdatei einschließlich Fehler, falls vorhanden.Python: stdout zu beiden Konsole und Textdatei einschließlich Fehler

Bisher meine Versuche sind diese:

Mit Konsole:

# mystdout.py 
# note that it has missing) sign 
print("hello 


# in the terminal: 
chmod a+x mystdout.py; ./mystdout.py 2>&1 | tee output.txt 
# does not print to oputut.txt if mystout.py has syntax errors 

Ausgabe in Datei (python3):

with open('out.txt', 'a') as f: 
    print('hello world', file=f) 
    # this does not print to console, only to the file 

eine Klasse definieren namens „T "

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# Author : Bhishan Poudel 
# Date  : Jul 12, 2016 


# Imports 
import sys 
import subprocess 

##============================================================================= 
class Tee(object): 
    def __init__(self, *files): 
     self.files = files 
    def write(self, obj): 
     for f in self.files: 
      f.write(obj) 
      f.flush() 
    def flush(self) : 
     for f in self.files: 
      f.flush() 

f = open('out.txt', 'w') 
original = sys.stdout 
sys.stdout = Tee(sys.stdout, f) 
##============================================================================= 
print('This works good, prints all the output to both console and to a file') 
print("This does not print output to file in case of syntax errors") 
print("This does not print output of subprocess.call") 

Frage Angenommen, ich habe eine ausführbare Datei (von C-Programm, das druckt hallo)

subprocess.call('./hello') 
# How to print output of this executable to both console and outputfile? 

Hinweis: Code zu erzeugen ausführbaren hallo

// gcc -o hello hello.c 
#include<stdio.h> 

int main() { 
    printf("hello\n"); 
return 0; } 

Weiterführende Links:
How to redirect 'print' output to a file using python?
http://linux.byexamples.com/archives/349/how-to-redirect-output-to-a-file-as-well-as-display-it-out/
Output on the console and file using python

+0

Ihr Python-Skript keinen shebang hat. Wie stellt das System fest, dass Python es ausführt? – jpmc26

+0

@ jpmc26, mein Python-Skript hat Shebang, am Anfang des Skripts, #!/Usr/bin/env python oder, sollte ich folgendes verwenden: #!/Usr/bin/python? –

+0

Ihr Beispiel 'mystdout.py' Skript in dieser Frage hat keins. Die richtige Einstellung hängt von Ihrer Umgebungskonfiguration ab. – jpmc26

Antwort

4

Wenn Sie bash (mindestens Version 4) verwenden, können Sie ausführen: ./mystdout |& tee output.txt. Ansonsten sollte auch dein Vorschlag ./mystdout 2>&1 | tee output.txt funktionieren.

+0

Ich habe versucht: ./mystdout.py | & tee output.txt –

+0

Auch wenn Sie '| &'? – Pierre

+0

Bash: Syntaxfehler in der Nähe von unerwartetem Token '& ' –

3

Lösung von @Pierre sollte funktionieren. Sonst würde ich vorschlagen, stdout/stderr vom externen Prozess abzufangen und die Protokollierung mit zwei Handlern zu benutzen: einen für die Konsole, einen anderen für die spezifische Datei. Hier ist und example der Protokollkonfiguration.

+0

Wichtig, wenn Sie "Produktions" -Code und nicht nur ein schnelles Skript schreiben: Diese Antwort ist bei weitem eine bessere langfristige Antwort als meine. – Pierre

1

Ich benutze Bash 3.2.53. Leider hat die Lösung von Pierre bei mir nicht funktioniert. Und die Lösung von grundic war zu kompliziert für mich.

So kam ich mit der einfachen Lösung:
Wenn der Python Lauf ohne Fehler, diese Methode für mich gearbeitet:

python3 a.py | tee aout.txt 

# to test other example 
echo hello this is a test | tee hello.txt 

# Like this it works for every commands. 
Verwandte Themen