[英]Code Breaks Out of Iteration - Only Printing First Item
I have folders in a directory named 2001,2002,2003,2004,2005.我在名为 2001,2002,2003,2004,2005 的目录中有文件夹。 However when I iterate through them inside the 'for batch in batchno: print(batch)' It only prints the first one 2001. If I print the parameter "Batchno", it gives me all folders.
但是,当我在 'for batch in batchno: print(batch)' 中遍历它们时,它只打印第一个 2001。如果我打印参数“Batchno”,它会给我所有文件夹。 IS there something breaking out of the loop that I am missing?
有什么东西打破了我遗漏的循环吗? I read that a return can break out of your loop, not sure if "return filename" is doing that.
我读到 return 可以跳出你的循环,不确定“return filename”是否这样做。
def getCOAFilePaths(batchno, pdkey):
for date in pdkey:
date = date
for batch in batchno:
print(batch)
filepath = f'\\\test/User/000/000/{date}/IMGCD/{batch}/'
abspth = os.path.abspath(filepath)
for row in os.listdir(filepath):
print(abspth+"\\"+row)
logging.info(abspth+"\\"+row)
return filepath
makePdf(filepath, r'C:\Users\TEST\COA\misc.fof')
def makePdf(filepath, SaveToDir):
os.chdir(filepath)
try:
for j in os.listdir(os.getcwd()):
os.chdir(filepath)
fname, fext= os.path.splitext(j)
newfilename = fname + ".pdf"
im = Image.open(fname + fext)
os.chdir(SaveToDir)
im.save(newfilename, "PDF", resolution=100.0)
except Image.UnidentifiedImageError:
print(f"{fname+fext} found. Skipping UnidentifiedImageError error because this library cannot open a .db file "
f"and convert it to pdf.")
makePdf(filepath, r'C:\Users\TEST\COA\misc.fof')
filepath = getCOAFilePaths(x1, x5)
output:输出:
2001
expected output:预期输出:
2001,2002,2003,2004,2005
Absolutely!绝对地! It's hitting your return at the end of the first iteration of the for loop
它在 for 循环的第一次迭代结束时击中了你的回报
If I'm interpreting what you're attempting to do correctly, you can simply remove the return and your code should run fine如果我正在解释您尝试正确执行的操作,您只需删除返回值,您的代码应该可以正常运行
Yes, a return will end execution of a loop.是的,返回将结束循环的执行。 Try accumulating your result values and return them together after the
for
has completed.尝试累积您的结果值并在
for
完成后将它们一起返回。
Example using a list to accumulate the values使用列表累积值的示例
filepaths = [] # Create an empty list
for batch in batchno:
print(batch)
filepath = f'\\\test/User/000/000/{date}/IMGCD/{batch}/'
abspth = os.path.abspath(filepath)
for row in os.listdir(filepath):
print(abspth+"\\"+row)
logging.info(abspth+"\\"+row)
filepaths.append(filepath) # Accumulate values here
return filepaths # Return the list once completed the loop
Edit:编辑:
In case you only need those values to call the makePdf
function you can also directly call the function inside the loop, without returning anything如果您只需要这些值来调用
makePdf
函数,您也可以直接在循环内调用该函数,而无需返回任何内容
for batch in batchno:
print(batch)
filepath = f'\\\test/User/000/000/{date}/IMGCD/{batch}/'
abspth = os.path.abspath(filepath)
for row in os.listdir(filepath):
print(abspth+"\\"+row)
logging.info(abspth+"\\"+row)
makePdf(filepath, r'C:\Users\TEST\COA\misc.fof')
您在 for 循环中使用了“return”,以便在第一次迭代结束时它将从函数中出来
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.