Python 在while循环中使用生成器,并在每次生成后进行评估
我正在使用Python在CAD程序中打开一些文件。由于当我一次打开太多文件时,程序将崩溃,因此我希望脚本在文件大小总和超过某个值时停止打开我生成的列表中的文件 以下是我到目前为止的情况: 我正在将日志文件转换为列表。它包含以逗号分隔的文件路径:Python 在while循环中使用生成器,并在每次生成后进行评估,python,while-loop,iteration,generator,Python,While Loop,Iteration,Generator,我正在使用Python在CAD程序中打开一些文件。由于当我一次打开太多文件时,程序将崩溃,因此我希望脚本在文件大小总和超过某个值时停止打开我生成的列表中的文件 以下是我到目前为止的情况: 我正在将日志文件转换为列表。它包含以逗号分隔的文件路径: fList = [] with open('C:/Users/user/Desktop/log.txt', 'r') as f: fList = f.read().split(',') with suppress(ValueError,
fList = []
with open('C:/Users/user/Desktop/log.txt', 'r') as f:
fList = f.read().split(',')
with suppress(ValueError, AttributeError):
fList.remove('')
fcount = len(fList)
这是我用来迭代partList的生成器:
def partGenerator(partList):
for file in partList:
yield file
在这里,当文件大小的总和小于2500000比特时,我尝试循环文件:
count = 0
progression = 0
storage = 0
while storage < 2500000:
for file in partGenerator(fList):
name = os.path.basename(file)
storage += os.path.getsize(file)
print(f'Auslastung: {storage} bite / 2500000 bite')
oDoc = oApp.Documents.Open(file)
progression += 1
percent = round(100 * progression / fcount)
print(f'Fortschritt: {progression} / {fcount} ({percent} %) - {name}')
count=0
级数=0
存储=0
存储容量<2500000时:
对于零件生成器(fList)中的文件:
name=os.path.basename(文件)
存储+=os.path.getsize(文件)
打印(f'Auslastung:{storage}位/2500000位)
oDoc=oApp.Documents.Open(文件)
级数+=1
百分比=四舍五入(100*progression/fcount)
打印(f'Fortschritt:{progression}/{fcount}({percent}%)-{name})
发生的情况是,文件在CAD软件中正确打开,但在超过while条件后不会停止。我的猜测是,while条件是在列表中的条目用完之后计算的,而不是在每个条目之后计算的,就像我想做的那样
帮助正确的语法将是伟大的
我最终想要的是:
我希望使用此脚本的方式是,它打开一些文件,每当我在CAD程序中手动关闭一个文件时,它都会打开列表中的下一个文件,直到列表用尽。您的
,而条件从不被检查,因为for
循环从不允许Python检查。for
循环从生成器函数中获取元素既不在这里也不在那里
如果您的情况仍然存在,您需要检查您的内的循环:
for file in partGenerator(fList):
name = os.path.basename(file)
storage += os.path.getsize(file)
if storage >= 2500000:
# wait for input before continuing, then reset the storage amount
input("Please close some files to continue, then press ENTER")
storage = 0
Python不会检查while
条件,直到while…:
语句下的块中的完整套件(一系列语句)完成运行或执行continue
语句,因此while
条件在这里确实不合适
在上面的示例中,我使用了低技术的input()
函数,要求运行脚本的人在之后按ENTER
。这将取决于oDoc.Documents
实际提供的API,看看您是否可以使用它来检测文件是否已关闭
如果要使用生成器函数,请让它跟踪文件大小。您甚至可以让它从CSV文件中读取这些内容。顺便说一下,我会使用来处理拆分和进度:
import csv
def parts(logfile):
with open(logfile, newline='') as f:
reader = csv.reader(f)
files = [column for row in reader for column in row if column]
fcount = len(files)
storage = 0
for i, filename in enumerate(files):
storage += os.path.getsize(file)
if storage >= 2500000:
input("Please close some files to continue, then press ENTER")
storage = 0
print(f'Auslastung: {storage} bite / 2500000 bite')
yield file
print(f'Fortschritt: {i} / {fcount} ({i / fcount:.2%}) - {name}')
那就用
for file in parts('C:/Users/user/Desktop/log.txt'):
oDoc = oApp.Documents.Open(file)
请注意,打开的文件的绝对数量是您的操作系统所限制的,而不是这些文件的大小。根据Martijn Pieters的输入,我想出了一些非常适合我的方法。我是个编程高手,所以我花了一段时间才理解这个问题。以下是最后做得很好的地方:
fList = []
with open('C:/Users/jhoefler/Desktop/log.txt', 'r') as f:
fList = f.read().split(',')
with suppress(ValueError, AttributeError):
fList.remove('')
fcount = len(fList)
count = 0
progression = 0
for file in fList:
name = os.path.basename(file)
if oApp.Documents.Count < 10:
oDoc = oApp.Documents.Open(file)
else:
pCount = oApp.Documents.LoadedCount
fCount = oApp.Documents.LoadedCount
while fCount == pCount:
time.sleep(1)
pCount = oApp.Documents.LoadedCount
oDoc = oApp.Documents.Open(file)
progression += 1
percent = round(100 * progression / fcount)
print(f'Fortschritt: {progression} / {fcount} ({percent} %) - {name}')
fList=[]
将open('C:/Users/jhoefler/Desktop/log.txt','r')作为f:
fList=f.read().split(',')
使用抑制(ValueError,AttributeError):
fList.remove(“”)
fcount=len(fList)
计数=0
级数=0
对于fList中的文件:
name=os.path.basename(文件)
如果oApp.Documents.Count<10:
oDoc=oApp.Documents.Open(文件)
其他:
pCount=oApp.Documents.LoadedCount
fCount=oApp.Documents.LoadedCount
当fCount==pCount时:
时间。睡眠(1)
pCount=oApp.Documents.LoadedCount
oDoc=oApp.Documents.Open(文件)
级数+=1
百分比=四舍五入(100*progression/fcount)
打印(f'Fortschritt:{progression}/{fcount}({percent}%)-{name})
我相信有一种更优雅的方法可以解决这个问题,但它可以满足我的需要。为什么不关闭打开的文件?partGenerator的作用是什么?您可以直接在列表上迭代,无需生成器。我手动对文件进行了一些小更改,因此无法让脚本关闭文件本身。我希望脚本在以前打开的文件的文件大小超过某个值时,在列表上暂停迭代。我的猜测是,这只可能与一个生成器有关。对不起,在收到解决您最初问题的答案后,请不要更改问题。如果你在解决第一个问题后还有更多问题,请发布一个新问题。谢谢你的回答!我也考虑过使用if条件,但是脚本在满足大小条件后就停止了,对吗?如果可能的话,我希望脚本暂停,直到我关闭足够多的文件,以便它可以打开下一个文件,直到列表被删除exhausted@J.H:然后不要退出,暂停。使用input('已达到大小限制,要继续吗?')
@J.H:是的,如果您在状态下使用,当该状态不再为真时,循环将退出,这就是这样一个循环的点。如果不想退出循环,请不要使用while
循环。好的,我想我知道了。谢谢你的意见!