問題描述
我的程序有一個錯誤:(
I have a bug in my program :(
問題是:
- 我的
.py
代碼很長,需要很長時間才能運行 - 我不知道錯誤在哪里
- My
.py
code is long, and takes ages to run - I don't know where the bug is
好消息是我的 py
文件中有很多 print()
,所以我可能知道錯誤在哪里生活.
The good news is that I have a lot of print()
in my py
file, so I can potentially know where the bug lives.
壞消息是我的錯誤導致我的計算機崩潰,所以我無法查看 ipython
控制臺,看看出了什么問題.
The bad news is that my bug makes my computer crash, so there is no way for me to look at the output of the ipython
console and see what went wrong.
如何在程序運行時將輸出寫入磁盤?這樣我在重啟后仍然可以打開文件以了解崩潰前發生了什么?
How can I have the output be written to disk while the program runs? So that I can still open the file after reboot to understand what happened before the crash?
這個問題不同于 將標準輸出重定向到 Python 中的文件?,因為我需要
This question is different from Redirect stdout to a file in Python?, because I need
- 連續寫入文件
- 在 Spyder 中使用的東西
非常感謝!
推薦答案
非常有趣的問題!幸運的是,IPython 為您提供了正確的魔法.它被稱為 %logstart
(請點擊鏈接查看完整文檔).
Very interesting question! Fortunately IPython has the right magic for you. It's called %logstart
(please follow the link for the full documentation).
要開始使用它并保存所有命令的輸入和輸出,只需輸入 IPython 控制臺
To start using it and save the input and output of all your commands, just type in an IPython console
In[1]: %logstart -o
這會將您從那一刻起的會話記錄到您當前目錄中名為 ipython_log.py
的文件中.
and that will record your session from that moment on into a file called ipython_log.py
placed in your current directory.
%logstart
非常靈活,因此您可以選擇不同的文件來保存,以及保存會話的方式(純 Python 或 IPython 命令).
%logstart
is very flexible, so you can select a different file to save to, and also how you save your session (either pure Python or as IPython commands).
這篇關于如何將 IPython 控制臺的輸出保存到 Spyder 中的文件中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!