Python 文件I/O

打开文件并写内容:

# _*_ coding: gbk _*_
# 打开一个文件
fo = open("foo.txt", "wb")
fo.write (("Python isia great language.
Yeah its great!!
").encode()) 
#注意上面的内容添加encode()否则报错:TypeError: 'str' does not support the buffer interface
 
# 关闭打开的文件
fo.close()

打开文件并读内容:

fo = open("foo.txt", "r+")
str = fo.read(10);
print ("Read String is : ", str)
# 关闭打开的文件
fo.close()

http://www.w3cschool.cc/python/python-files-io.html

原文地址:https://www.cnblogs.com/lwngreat/p/4186699.html