![](/img/trans.png)
[英]Keras generator and fit_generator, how to build the generator to avoid 'function shape' error
[英]how to avoid error of adding string to generator expressions?
我需要遍歷文件夾名稱,然后遍歷圖像,但出現此錯誤。 有人可以告訴我如何避免錯誤嗎?
path = '/.../'
dirs = next(os.walk(path))[1] # get my folder names inside my directory
for i in dirs:
for img in os.listdir(path+(x for x in dirs)): <------ TypeError: must be str, not generator
img_path = os.path.join(path,img)
print(img_path)
該錯誤來自您試圖向Generator exp添加path
的上一行:
path+(x for x in dirs)
您應該使用os.path.join
將path
連接到目錄名稱:
for dir in dirs:
for img in os.listdir(os.path.join(path, dir)):
...
import os
path = '/home/'
dirs = next(os.walk(path))[1] # get folder names inside directory
for i in dirs:
for img in os.listdir(path+i):
img_path = os.path.join(path,img)
print(img_path)
在下面的行中,您嘗試將生成器對象和字符串path
串聯起來。 相反,您可以如上所述使用i
本身。
path+(x for x in dirs)
您不需要使用listdir
使代碼復雜化。 這個:
import os, os.path
path = '/.../'
for d, _, files in os.walk(path):
for f in files:
img_path = os.path.join(d, f)
print(img_path)
應該足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.