[英]In python WindowsError: [Error 3] The system cannot find the path specified: 'F:/Dataset\\*.*'
for category in CATEGORIES: # do dogs and cats
path = os.path.join(DATADIR,category) # create path to dogs and cats
for img in os.listdir(path): # iterate over each image per dogs and cats
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
plt.imshow(img_array, cmap='gray') # graph it
plt.show() # display!
error occurred:发生了错误:
WindowsErrorTraceback (most recent call last)
<ipython-input-4-be2d68025f07> in <module>()
1 for category in CATEGORIES: # do dogs and cats
2 path = os.path.join(DATADIR,category) # create path to dogs and cats
----> 3 for img in os.listdir(path): # iterate over each image per dogs and cats
4 img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
5 plt.imshow(img_array, cmap='gray') # graph it
WindowsError: [Error 3] The system cannot find the path specified: 'F:/Dataset\\Bening\\*.*'
WindowsError: [Error 3] The system cannot find the path specified: 'F:/Dataset\\Bening\\*.*'
The error literally says the path is wrong.该错误字面意思是路径错误。
Let's look more closely:让我们更仔细地看一下:
We have os.listdir(path)
with an error caused by 'F:/Dataset\\Bening\\*.*'
我们有一个由
'F:/Dataset\\Bening\\*.*'
引起的错误的os.listdir(path)
Firstly, paths in Python don't use wildcards .首先, Python 中的路径不使用通配符。 .
. It's an
os.listdir
, so you pass a directory in there.这是一个
os.listdir
,所以你在那里传递一个目录。 If you want to use wildcards, try glob
.如果您想使用通配符,请尝试
glob
。
Secondly, you use wrong separator in your definition of DATADIR
- os.path.join
uses the system-specific one which is backslash in Windows (double backslash in print because it needs escaping), you use a normal, forward slash.其次,您在
DATADIR
的定义中使用了错误的分隔符 - os.path.join
使用系统特定的分隔符,即 Windows 中的反斜杠(打印中的双反斜杠,因为它需要转义),您使用正常的正斜杠。 You can put the path with backslashes by putting r
just before opening the string (it means the string is raw - backslashes are literal, not an escaping sequence) or by doubling the backslashes manually.您可以通过在打开字符串之前放置
r
来放置带有反斜杠的路径(这意味着字符串是原始的 - 反斜杠是文字,而不是 escaping 序列)或手动加倍反斜杠。 r'F:\Dataset'
or 'F:\\Dataset
r'F:\Dataset'
或'F:\\Dataset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.