首页 > 代码库 > Python 文件I/O

Python 文件I/O

打开文件并写内容:

# _*_ coding: gbk _*_# 打开一个文件fo = open("foo.txt", "wb")fo.write (("Python isia great language.\nYeah its great!!\n").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

Python 文件I/O