python随机读取txt文件一行
直接贴出全部代码吧,新手开始学py啦~
import random
import os
import linecache
def find():
txt = open(os.getcwd()+'/cookie.txt','rb')
data = txt.read().decode('utf-8') #python3一定要加上这句不然会编码报错!
txt.close()
n = data.count('\n')
i = random.randint(1, (n+1))
print("本次使用的行数",i) #我的文档我数了一下是120行,这里就是1~121
###得到对应的i行的数据
line=linecache.getline(os.getcwd()+'/cookie.txt',i)
return line
print(find())
一个自己的Test玩站网