2022年7月6日 星期三

stdout

 stdout

2022/07/03

-----


https://pixabay.com/zh/photos/dog-cat-pets-mammals-animals-2606759/

-----

# stdout

「其實 C 函式庫中已經宣告好 3 個 FILE * 的指標,分別是 stdin、stdout、stderr。」[1]。


# stdout

# fd

「生活中常常會需要把 stdout/stderr 給關掉,以避免程式沒寫好然後莫名跳了一堆錯誤訊息造成客戶困擾,當今天關閉了如何重新開啟呢?會有這樣的需求其實也很有趣,因為 fork 出一個 process 會繼承 parent 的一切 attribute,包含 stdout/stderr 的 fd: 1 & 2,因此若是 parent 想關但 child 想開就會有這樣的需要。」[2]。


# fd

「file descriptor (fd) 基本上是一層介面,可以讓我們去操作 file 和其他 input/output interface(例如 pipe & socket)。」[3]。


# 0 1 2

「在 C 語言 中,標準輸入、標準輸出和標準錯誤輸出分別連接到已存的 Unix 檔案描述子 0 、1 和 2。」[4]。


# 0 1 2

「1. 0:Standard Input (STDIN),預設為鍵盤輸入值。

 2. 1:Standard Output (STDOUT),預設為終端機螢幕顯示輸出。

 3. 2:Standard Error (STDERR),預設為終端機螢幕顯示輸出。」[5]。


# stdout 0 1 2

「摘要:【筆記整理】標準輸入輸出和管線 (Standard Input Output and Pipes)」[6]。


# stdout

「old_stdout = sys.stdout

sys.stdout = open('log.txt','a')

print('hey')

sys.stdout.close()

sys.stdout = old_stdout」[7]。


# __stdout__

「在 Python 中,檔案物件 sys.stdin、sys.stdout 和 sys.stderr 分別對應直譯器的標準輸入、標準輸出和標準出錯流。在程式啟動時,這些物件的初值由 sys.__stdin__、sys.__stdout__ 和 sys.__stderr__ 儲存,以便用於收尾(finalization)時恢復標準流物件。」[8]。


# sys.stdout __stdout__

「stdin is used for all interactive input (including calls to input());

stdout is used for the output of print() and expression statements and for the prompts of input();

The interpreter’s own prompts and its error messages go to stderr.」[9]。

「sys.__stdin__

sys.__stdout__

sys.__stderr__

These objects contain the original values of stdin, stderr and stdout at the start of the program. They are used during finalization, and could be useful to print to the actual standard stream no matter if the sys.std* object has been redirected.」[9]。

-----

References


# sys.stdout

[1] python中sys.stdout和sys.stderr_kunkun_1230的博客-CSDN博客_sys.stderr

https://blog.csdn.net/weixin_44731100/article/details/88642269


[2] Input / Output (輸入輸出) - 從零開始的開源地下城 - HackMD

https://hackmd.io/@combo-tw/r19DJIRBr


# stdout

[3] 如何重新開啟stdout? | 菜鳥的三年成長史

https://wirelessr.gitbooks.io/working-life/content/ru_he_zhong_xin_kai_qi_stdout.html


# linux fd

[4] Linux 的 file descriptor 筆記 - Kakashi's Blog

https://kkc.github.io/2020/08/22/file-descriptor/


# stdout

[5] 標準串流 - 維基百科,自由的百科全書

https://zh.wikipedia.org/wiki/%E6%A8%99%E6%BA%96%E4%B8%B2%E6%B5%81


# stdout 0 1 2

[6] 【筆記整理】標準輸入輸出和管線 (Standard Input Output and Pipes) | ghoseliang - 點部落

https://dotblogs.com.tw/ghoseliang/2013/05/29/105049


# stdout

[7] python - sys.stdout not reassigning to sys.__stdout__ - Stack Overflow

https://stackoverflow.com/questions/12719328/sys-stdout-not-reassigning-to-sys-stdout


# __stdout__

[8] 詳解python的幾種標準輸出重定向方式 | 程式前沿

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/368212/


# sys.stdout __stdout__

[9] sys — System-specific parameters and functions — Python 3.10.5 documentation

https://docs.python.org/3/library/sys.html

-----

鐵達尼號 Python 實作(目錄)

https://mandhistory.blogspot.com/2022/06/titanic.html

-----

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。