count = 0
file_dirs = ('./qrcode_path/qrcode_ID.txt')
filename = open(file_dirs, 'r') # 以只读方式打开文件
file_contents = filename.read() # 读取文档内容到file_contents
for file_content in file_contents: # 统计文件内容中换行符的数目
if file_content == '\n':
count += 1
if file_contents[-1] != '\n': # 当文件最后一个字符不为换行符时,行数+1
count += 1
if count >= 10000:
f = open('./qrcode_path/qrcode_ID.txt', "r+")
f.truncate()
print('超过ID缓存规定行数 10000+,ID缓存已清空')
file_dirs = ‘这里天你的txt文本路径’
if count>=10000: 这里判断txt文本行数内容是否大于10000行
代码中有借鉴之处,如被借鉴者不愿,可私信我删除