[英]How to iterate through a fixed number of files until the last file of the folder?
我需要迭代的文件夾中有大量文本文件(超過2000個)。 這是我目前設法執行的操作:
import os
filepath='E:\Data'
save_path='E:\Results'
for file in os.listdir(filepath):
if file.endswith('.txt'):
with open(os.path.join(filepath,file),'r') as myfile:
for eachline in myfile:
MainID=eachline[:6]
if MainID=='AKJ':
for field in eachline.split():
MainID=field.split(',')[1]
Origin=field.split(',')[9]
Price=field.split(',')[13]
fo1=open(os.path.join(save_path,file),'a')
fo1.write('%s,%s,%s\n' %(MainID,Origin,Price))
fo1.close()
但是,我需要對前100個文件,然后對隨后的100個文件等執行我的過程,直到文件夾結束,而不是像上面的代碼一樣一次遍歷所有文件直到結束。 任何幫助,將不勝感激。
files = [file for file in os.listdir(filepath) if file.endswith('.txt')]
batchsize = 100
index = 0
remaining = len(files)
while remaining > 0:
batch = min(remaining, batchsize)
print('NEW BATCH')
for file in files[index:index+batch]:
with open(os.path.join(filepath, file), 'r') as myfile:
print(' ', file)
index += batch
remaining -= batch
complete_file_paths = [os.path.join(filepath,file) for file in os.listdir(filepath)]
chunks_of_100 = (complete_file_paths[i:i+100] for i in range(0,len(complete_file_paths),100))
for chunk in chunks_of_100:
print chunk
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.