数据保存txt

import os 

# 用于写入文件,a模式代表追加
def add_data(dataFileName, dataMsg):
	with open(dataFileName, 'a') as f:
		f.write(dataMsg)

# 用于检测数据是否已经存在于文件中
def check_exist(dataFileName, dataMsg)
	# 如果文件不存在先创建文件
	if os.path.exists(dataFileName) == False:
		with open(dataFilename, 'w') as fp:
			print("成功创建文件:"+ dataFileName)
	isExist = False
	# 默认设定数据不存在于文件中
	with open(dataFileName, 'r') as f:
		lines = f.readlines()
		# 将文件整个读取出来
		for line in lines:
			# 一行一行的比较
			if line.replace('\n','') == dataMsg:
				isExist = True
				return isExist
				# 如果有相同的文件立即return就不需要继续比较了
	return isExist

猜你喜欢

转载自blog.csdn.net/u012700515/article/details/112911582