我已經注意到,httpie
蟒蛇工具給出不同的結果在以下兩個CAES:如何在python中將不同的結果打印到屏幕和文件中?
$ http google.com
$ http google.com > out.txt
out.txt
錯過存在於第一種情況下頭文件。
我已經注意到,httpie
蟒蛇工具給出不同的結果在以下兩個CAES:如何在python中將不同的結果打印到屏幕和文件中?
$ http google.com
$ http google.com > out.txt
out.txt
錯過存在於第一種情況下頭文件。
論http
手冊頁面,您可以找到以下
Output options:
--print WHAT, -p WHAT
String specifying what the output should contain:
'H' request headers 'B' request body 'h' response headers 'b' response body
The default behaviour is 'hb' (i.e., the response headers and body is
printed), if standard output is not redirected. If the output is piped
to another program or to a file, then only the response body is printed by
default.
指示每當輸出重定向該http
故意行爲不同。爲了獲得相同的行爲不重定向輸出,您可以使用
`http --print hb google.com > out.txt`
(但也注意到,漂亮的印刷與重定向行爲有所不同。)
哎呀,看到這個答案被接受了,我想我的答案完全沒有了。在編寫Python程序時,我認爲這個問題是關於「如何將不同的結果打印到屏幕和文件」。 : - ] –
使用sys.stdout.isatty
告訴stdout
是否是終端(「TTY」)或文件,並根據打印不同的輸出,例如:
import sys
if sys.stdout.isatty():
print "Hello terminal!"
else:
print "Hello non-terminal!"
我一樣的錯誤到底是什麼有點困惑。 –