python3.7 pycharm Pyqt5 获取txt文本行数 如果超过规定范围就删除txt文本内容

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行

代码中有借鉴之处,如被借鉴者不愿,可私信我删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值