没有这样的文件或目录:“builtup/build0.jpg”

2024-05-20 17:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

在我准备的satellite images文件夹中打印前9张图片,并键入文件夹路径“building”时,它会显示:

FileNotFoundError: [Errno 2] No such file or directory

enter image description here


Tags: orno路径文件夹键入图片directoryfile
3条回答

我会做一些像。。你知道吗

import os
filename = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 
    folder, 
    'build{}.jpg'.format(i)
)

使用图像的绝对路径。你知道吗

    import os
    absolute_path = os.path.join(os.getcwd(), 'builtup', 'build', str(i) + '.jpg');

您可以迭代给定文件夹中的所有文件并检查它们是否为.jpg/.jpg

from matplotlib import pyplot
from matplotlib.image import imread
import os

#also try with full path:
folder = '/builtup/'

i=0
for file in os.listdir(folder):
    if file.endswith('.jpg') or file.endswith('.JPG'):
        if i <= 8:
            pyplot.subplot(330 + 1 + i)
            image = imread(os.path.join(folder,file))
            pyplot.imshow(image)
            i=i+1

pyplot.show()

相关问题 更多 >