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')
这样就可以了,如果遇到中文编码报错再加编码格式就好了