Python Basics - File operations

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sinat_29006909/article/details/91371835

Python some basic file operations, such as read, write, etc.

打开文件

open(文件名,操作类型)函数是打开文件的操作这里有两个参数
操作类型:
w:写入(会覆盖之前的内容)
r:读取
b:二进制
a+:追加

#读取文件
file = open("f:/test.txt",'r') 	#打开文件
data = file.read()			    #读取文件
line = file.readline()		    #读取一行
file.close() 					#关闭文件

#写入文件(写入文件会覆盖之前的内容)
file = open("f:/test.txt",'w') 	#打开文件
file.write("写入一条数据")		#写入数据
file.close()					#关闭文件

#写入文件(追加写入)
file = open("f:/test.txt",'a+') 	#打开文件
file.write("写入一条数据")		#写入数据
file.close()					#关闭文件

Guess you like

Origin blog.csdn.net/sinat_29006909/article/details/91371835