biz*_*nez 1 python file-io stream
我知道Python中有一个StringIO流,但是在Python中有这样的文件流吗?还有更好的方法让我查看这些东西吗?文件等......
我试图将"流"传递给我制作的"作家"对象.我希望我可以将文件句柄/流传递给这个编写器对象.
我猜你正在寻找open().http://docs.python.org/library/functions.html#open
outfile = open("/path/to/file", "w")
[...]
outfile.write([...])
Run Code Online (Sandbox Code Playgroud)
关于您可以使用流做的所有事情的文档(这些在Python中称为"文件对象"或"类文件对象"):http://docs.python.org/library/stdtypes.html#file-objects
有一个内置文件(),其工作方式大致相同.以下是文档:http://docs.python.org/library/functions.html#file和http://python.org/doc/2.5.2/lib/bltin-file-objects.html.
如果要打印文件的所有行,请执行以下操作:
for line in file('yourfile.txt'):
print line
Run Code Online (Sandbox Code Playgroud)
当然还有更多,比如.seek(),. close(),. read(),. readlines(),...基本上和StringIO的协议相同.
编辑:您应该使用open()而不是file(),它具有相同的API - file()在Python 3中.
| 归档时间: |
|
| 查看次数: |
6686 次 |
| 最近记录: |