2014年03月27日 星期四 10:13
Python内置读写文件的基本操作函数,并且支持with语句,可以更加方便的管理相关的资源。
示例代码如下:
with open('d:/test.txt','rt') as rf: data=rf.read() print(data) with open('d:/test.txt',"at") as wf: wf.write("Hello Python!\n") wf.write("Hello PyDev!\n") print("Hello World!",file=wf) with open('d:/test.txt') as rf: for line in rf: print(line,end='')
参考资料:
http://docs.python.org/3/library/functions.html#open
http://docs.python.org/3/reference/compound_stmts.html#with
Zeuux © 2024
京ICP备05028076号