贴代码:
import os
import os.path
import json
import logger
import time
gar = []
file = u"E:/ybl_TestDepartment/interface_test/config/1.txt"
class TXT():
log = logger.Log()
def __init__(self): if not os.path.exists(file):
self.log.info("错误信息:文件不存在")
self.file = open(file,"r+") def Read(self):
gar = []
filen=self.file.readlines()
for i in filen:
gar.append(i.strip())
if (0<len(gar)<5):
self.log.info("##警告: phone_data即将为空,目前为%s##"%len(gar))
if(len(gar) ==0):
self.log.info("##警告: phone_data文件为空,请输入phone") return gar[0] def Del(self):
gar = []
filen=self.file.readlines()
for i in filen:
gar.append(i)
self.file.seek(0)
self.file.truncate()
self.file.close()
gar.pop(0)
#gar.append(" ")
self.file = open(file,"r+")
#self.file.truncate()
time.sleep(1) for i in gar:
self.file.write(i+"\n")
self.file.close()
此代码在:common/Txt.py
class register():
log = logger.Log()
s =requests.session() def __init__(self):
mysql = MysqlUtil()
A = TXT()
phone = A.Read() while True:
#验证取得的电话号码是否已经注册过
sql = "SELECT DISTINCT(phone) FROM table WHERE phone = '"+phone+"';"
#if (sql == "null"):
#print (sql)
result = mysql.mysql_getrows(sql)
result1 = json.dumps(result) if (result1 == '[]'):
self.phone =phone
print (self.phone)
break
else:
A = TXT()
A.Del()
phone = A.Read()
self.log.info("####号码重复,重取####") 不喜勿喷,第一次在园里贴自己写的代码,
开始想在excel表里实现,但发现xlrt库写入和删除没有找到方法,算了,还是txt吧,反正功能能实现,就是打开txt丑点,哈哈哈哈哈哈哈哈哈哈哈哈
不让发布到博客园首页,那我再写几个字,