python輸出重定向到文件
Ⅰ python unittest測試框架輸出的log無法重定向到文件
看看這個:
http://bbs.chinaunix.net/thread-3772869-1-1.html
Ⅱ python 如何讓一個函數的輸出寫入到一個文件中
首先導入sys模塊
import sys
然後在打算把輸出數據寫入文件的代碼之前加上以下代碼
output=sys.stdout
outputfile=open(filename,'w')
sys.stdout=outputfile
上面的filename表示輸出文件
程序結束或恢復成正常輸出時加上以下代碼
outputfile.close()
sys.stdout=output
恢復輸出為開始保存的正常輸出值
Ⅲ 關於python的輸出重定向
importsys
f=open('a.txt','w')
print>>sys.stdout,'hello,world'
hello,world
print>>f,'hello,world'
f.close()
輸出到屏幕的內容重定向到文件,供參考。
defprint(stream):
"""print(value,...,sep='',end='\n',file=sys.stdout)
Printsthevaluestoastream,ortosys.stdoutbydefault.
Optionalkeywordarguments:
file:afile-likeobject(stream);defaultstothecurrentsys.stdout.
sep:stringinsertedbetweenvalues,defaultaspace.
end:,defaultanewline."""
pass
Ⅳ python標准輸出重定向stdout.py的意思
sys.stdout 默認就是輸出到控制台(console),print 默認的輸出也是 sys.stdout,所以輸出到控制台。
在 輸入B 那,做了上下文切換with open
,也就是把默認的輸出流指向到文件 out.log,
對應的代碼是: sys.stdout = self.out_new,這里 out_new -> out.log,out_old = console
所以就print 指向文件,而不是控制台了
離開語句時,執行 sys.stdout = self.out_old => sys.stdout = console,還原原來的默認輸入流
於是後面就輸出到默認的控制
Ⅳ 我正在自學python 請問「符號 >> 用來重定向輸出」是怎麼用的
print正常是要輸出到屏幕上,如果你希望輸出到其他地方比如文件,就需要使用>>來把輸出導向到文件。空格的目的是要區分關鍵詞,如果你輸入的是print>>,電腦會把它當成一個單詞,而無法知道是print函數。
Ⅵ python怎樣將運行結果寫入到文件里
你是說把控制台的所有輸出保存到文件?
用重定向
pythoncode.py>output.txt
Ⅶ python lib subprocess裡面是怎麼把shell的輸出重定向至某個文件內
#!/usr/bin/env python
#coding=utf-8
from subprocess import Popen, PIPE
import re
def disk_space(pattern="2[0-9]%", message="CAPACITY WARNING"):
## take shell command output
ps = Popen("df -h", shell=True, stdout=PIPE, stderr=PIPE)
output_lines = ps.stdout.readlines()
for line in output_lines:
line = line.strip()
if re.search(pattern, line):
print "%s %s" %(message, line)
if __name__ == '__main__':
disk_space()
Ⅷ python怎麼重定向輸入
控制台重定向
最簡單常用的輸出重定向方式是利用控制台命令。這種重定向由控制台完成,而與Python本身無關。
Windows命令提示符(cmd.exe)和Linux Shell(bash等)均通過">"或">>"將輸出重定向。其中,">"表示覆蓋內容,">>"表示追加內容。類似地,"2>"可重定向標准錯誤。重定向到"nul"(Windows)或"/dev/null"(Linux)會抑制輸出,既不屏顯也不存檔。
以Windows命令提示符為例,將Python腳本輸出重定向到文件(為縮短篇幅已刪除命令間空行):
E:\>echo print 'hello' > test.py
E:\>test.py > out.txt
E:\>type out.txt
hello
E:\>test.py >> out.txt
E:\>type out.txt
hello
hello
E:\>test.py > nul
注意,在Windows命令提示符中執行Python腳本時,命令行無需以"python"開頭,系統會根據腳本後綴自動調用Python解釋器。此外,type命令可直接顯示文本文件的內容,類似Linux系統的cat命令。
Linux Shell中執行Python腳本時,命令行應以"python"開頭。除">"或">>"重定向外,還可使用tee命令。該命令可將內容同時輸出到終端屏幕和(多個)文件中,"-a"選項表示追加寫入,否則覆蓋寫入。示例如下(echo $SHELL或echo $0顯示當前所使用的Shell):
[wangxiaoyuan_@localhost ~]$ echo $SHELL
/bin/bash
[wangxiaoyuan_@localhost ~]$ python -c "print 'hello'"
hello
[wangxiaoyuan_@localhost ~]$ python -c "print 'hello'" > out.txt
[wangxiaoyuan_@localhost ~]$ cat out.txt
hello
[wangxiaoyuan_@localhost ~]$ python -c "print 'world'" >> out.txt
[wangxiaoyuan_@localhost ~]$ cat out.txt
hello
world
[wangxiaoyuan_@localhost ~]$ python -c "print 'I am'" | tee out.txt
I am
[wangxiaoyuan_@localhost ~]$ python -c "print 'xywang'" | tee -a out.txt
xywang
[wangxiaoyuan_@localhost ~]$ cat out.txt
I am
xywang
[wangxiaoyuan_@localhost ~]$ python -c "print 'hello'" > /dev/null
[wangxiaoyuan_@localhost ~]$
若僅僅想要將腳本輸出保存到文件中,也可直接藉助會話窗口的日誌抓取功能。
注意,控制台重定向的影響是全局性的,僅適用於比較簡單的輸出任務。
Ⅸ python怎樣將結果輸出到文件中
dataframe推薦直接存成csv文件格式
data.to_csv('file_name.csv')
這樣就可以了,如果遇到中文編碼報錯再加編碼格式就好了