# 批量讀取txt行數
import os
import linecache
# 批量獲取txt行數
root=r'D:\Projects\wwlln\all\xinan\201601' # 文件所在路徑
file_names = os.listdir(root) # 讀取路徑下所有文件名
file_ob_list=[] # 創建一個列表,用於存儲文件絕對路徑名
for file_name in file_names:
filedir=root+'\\'+file_name
file_ob_list.append(filedir) # 將文件絕對路徑名放入列表中
print(file_ob_list)
filename=r'C:\Users\zm.com\Desktop\N201601.txt' #用於輸出行數的文件
with open(filename,'w') as f: #打開文件
for file in file_ob_list: #循環路徑下的文件
count=-1
for count,line in enumerate(open(file,'rU').readlines()): #獲取文件行數
count+=1
if (count==-1): #在此處判斷,如果文件無數據,讓count從-1變成0
count+=1
print(count)
f.writelines([str(file[-10:-4]),':',' ',str(count),'\r\n']) #寫入文件名稱(此處取消了路徑名和后綴)和對應的行數
f.close
參考鏈接:
https://www.cnblogs.com/isme-zjh/p/11585462.html
https://blog.csdn.net/iin729/article/details/71911240
https://blog.csdn.net/flying_sfeng/article/details/75009741
翻譯 朗讀 復制 正在查詢,請稍候…… 重試 朗讀 復制 復制 朗讀 復制 via 谷歌翻譯(國內) 譯
