Zanurkuj w Pythonie/Standardowy strumień wejścia, wyjścia i błędów: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
Nie podano opisu zmian
Piotr (dyskusja | edycje)
Linia 36:
This message will be logged instead of displayed
 
(W Windowsie, możesz wykorzystać polecenie <tt>type</tt>, zamiast <tt>cat</tt>, aby wyświetlić zawartość pliku.)
(On Windows, you can use type instead of cat to display the contents of a file.)
 
Jeśli jeszcze tego nie zrobiłeś, możesz pobrać [http://diveintopython.org/download/diveintopython-examples-5.4.zip ten i inne przykłady] użyte w tej książce.
If you have not already done so, you can download this and other examples used in this book.
 
#stdout.py
Linia 51:
fsock.close() #(7)
 
# To zostanie wypisane w interaktywnym oknie IDE (lub w terminalu, jeśli skrypt został uruchomiony w linii poleceń).
# This will print to the IDE “Interactive Window” (or the terminal, if running the script from the command line).
# Zawsze, zanim przekierujemy standardowe wyjście, przypisujemy gdzieś <tt>stdout</tt>, dzięki temu, będziemy potem mogli do niego normalnie wrócić.
# Always save stdout before redirecting it, so you can set it back to normal later.
# Otwieramy plik do zapisu. Jeśli plik nie istnieje, zostanie utworzone. Jeśli istnieje, zostanie nadpisany.
# Open a file for writing. If the file doesn't exist, it will be created. If the file does exist, it will be overwritten.
# Całe późniejsze wyjście zostanie przekierowane do pliku, który właśnie otworzyliśmy.
# Redirect all further output to the new file you just opened.
# Zostanie to wypisane tylko do pliku <tt>out.log</tt>; nie będzie widoczne w oknie IDE lub w terminalu.
# This will be “printed” to the log file only; it will not be visible in the IDE window or on the screen.
# Przywracamy <tt>stdout</tt> do początkowej postaci, zanim zaczęliśmy się w nim babrać.
# Set stdout back to the way it was before you mucked with it.
# CloseZamykamy theplik <tt>out.log file</tt>.
 
RedirectingPrzekierowywanie standardowego strumienia błędów (<tt>stderr</tt>) worksdziała exactlyw theten samesam waysposób, usingwykorzystując <tt>sys.stderr</tt>, instead ofzamiast <tt>sys.stdout</tt>.
 
'''ExamplePrzykład 10.10. RedirectingPrzekierowywanie errorinformacji informationo błędach'''
 
[you@localhost kgp]$ python stderr.py
Linia 70:
Exception: this error will be logged
 
Jeśli jeszcze tego nie zrobiłeś, możesz pobrać [http://diveintopython.org/download/diveintopython-examples-5.4.zip ten i inne przykłady] użyte w tej książce.
If you have not already done so, you can download this and other examples used in this book.
 
#stderr.py
Linia 79:
raise Exception, 'this error will be logged' #(3) (4)
 
# Otwieramy plik <tt>error.log</tt>, gdzie chcemy przechowywać informacje debugujące.
# Open the log file where you want to store debugging information.
# Przekierowujemy standardowy strumień błędów, dzięki przypisaniu obiektu pliku nowo otwartego pliku do <tt>sys.stderr</tt>.
# Redirect standard error by assigning the file object of the newly-opened log file to stderr.
# Rzucamy wyjątek. Zauważmy, że na ekranie wyjściowym nic nie zostanie wypisane. Wszystkie informacje ''traceback'' zostały zapisane w <tt>error.log</tt>.
# Raise an exception. Note from the screen output that this does not print anything on screen. All the normal traceback information has been written to error.log.
# Zauważmy także, że nie zamkneliśmy wyraźnie pliku log, a nawet nie przypisaliśmy do <tt>sys.stderr</tt> jest pierwotnej wartości. To jest wspaniałe, że kiedy program się rozwali (z powodu wyjątku), Python wyczyści i zamknie wszystkie pliki za nas. Nie ma żadnej różnicy, czy <tt>stderr</tt> zostanie przywrócony, czy też nie, ponieważ program się rozwala, a Python kończy działanie. Przywrócenie wartości do orginalnej, jest bardziej ważne dla <tt>stdout</tt>, jeśli zamierzasz później wykonywać jakieś inne operacje w tym samym skrypcie.
# Also note that you're not explicitly closing your log file, nor are you setting stderr back to its original value. This is fine, since once the program crashes (because of the exception), Python will clean up and close the file for us, and it doesn't make any difference that stderr is never restored, since, as I mentioned, the program crashes and Python ends. Restoring the original is more important for stdout, if you expect to go do other stuff within the same script afterwards.
 
Ponieważ powszechnie wypisuje się informacje o błędach na standardowy strumień błędów, Python posiada skrótową składnie, która można wykorzystać do bezpośredniego przekierowywania wyjścia.
Since it is so common to write error messages to standard error, there is a shorthand syntax that can be used instead of going through the hassle of redirecting it outright.
 
'''ExamplePrzykład 10.11. PrintingWypisywanie todo <tt>stderr</tt>'''
 
>>> print 'entering function'
Linia 94:
entering function
 
# Ta skrótowa składnia wyrażenia <tt>print</tt> może być wykoryzstywane do pisania do dowolnego, otwartego pliku, lub do ''obiektu pliko-podobnego''. W tym przypadku, możemy przekierować pojedynczą instrukcję <tt>print</tt> do <tt>stderr</tt> bez wpływu na następne instrukcje <tt>print</tt>.
# This shorthand syntax of the print statement can be used to write to any open file, or file-like object. In this case, you can redirect a single print statement to stderr without affecting subsequent print statements.
 
Standard input, on the other hand, is a read-only file object, and it represents the data flowing into the program from some previous program. This will likely not make much sense to classic Mac OS users, or even Windows users unless you were ever fluent on the MS-DOS command line. The way it works is that you can construct a chain of commands in a single line, so that one program's output becomes the input for the next program in the chain. The first program simply outputs to standard output (without doing any special redirecting itself, just doing normal print statements or whatever), and the next program reads from standard input, and the operating system takes care of connecting one program's output to the next program's input.