我正在嘗試使用 matplotlib 從我的檔案夾中列印一堆圖片,但是我不確定如何解決此錯誤。
基本上,我正在嘗試使用模式 ' _ 0 _ ' 或 ' _ 1 _ ' 獲取特定檔案,但它給了我一個錯誤并且不知道如何修復它。
我放了我試圖復制的代碼示例的螢屏截圖,如果您有任何問題,請告訴我。歡迎任何幫助!
import os
import matplotlib.pyplot as plt
from matplotlib.image import imread
#folder = '/content/drive/MyDrive/Colab Notebooks/data/dogs-vs-cats/train/'
plt.figure(figsize=(10,10))
for i in range(10):
plt.subplot(4,5,i 1)
filename = glob.glob(os.path.join(folder,'*_1_*.jpg'))
#for filename in folder:
#filename = glob.glob(folder '*_1_*.jpg')
#filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
#filename = folder '*_1_*.jpg'
if not os.path.exists(filename):
#if not os.path.exists(filename):
print ('No such file:' filename)
image = imread(filename)
plt.imshow(image)
for i in range(10):
plt.subplot(4,5,i 11)
plt.subplot(4,5,i 11)
plt.subplot(4,5,i 11)
filename = glob.glob(os.path.join(folder,'*_0_*.jpg'))
#filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_0_*.jpg')
#filename = folder ' *_0_*.jpg'
image = imread(filename)
plt.imshow(image)
plt.show()
錯誤資訊:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-8c96486bb2b7> in <module>()
11 #filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
12 #filename = folder '*_1_*.jpg'
---> 13 if not os.path.exists(filename):
14 #if not os.path.exists(filename):
15 print ('No such file:' filename)
/usr/lib/python3.7/genericpath.py in exists(path)
17 """Test whether a path exists. Returns False for broken symbolic links"""
18 try:
---> 19 os.stat(path)
20 except OSError:
21 return False
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list
我要完成的作業:

uj5u.com熱心網友回復:
該glob.glob()函式回傳一個串列,錯誤訊息告訴您
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list
你的回圈應該是這樣的,而不是:
for i in range(10):
plt.subplot(4, 5, i 1)
filelist = glob.glob(os.path.join(folder, '*_1_*.jpg'))
to_process = []
for entry in filelist:
if not os.path.exists(entry):
print ("File {entry} does not exist".format(entry=entry))
continue
else:
to_process.append(entry)
for entry in to_process:
# ... other desired processing
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/428947.html
標籤:Python matplotlib 小路 操作系统
