【问题标题】:Python: FileNotFoundError: [Errno 2] No such file or directory - How to add file to the right directoryPython:FileNotFoundError:[Errno 2] 没有这样的文件或目录 - 如何将文件添加到正确的目录
【发布时间】:2020-07-19 13:39:10
【问题描述】:

我是 python 新手!看到许多与此问题相关的问题,但找不到正确的方法。 我想导入一张图片并更改它。

我的代码是:

from PIL import Image, ImageFilter
import os

root_dir= os.path.dirname(os.path.abspath(r'C:\Users\User\eclipse-workspace\Practice Python CS50 2019\images\Mario.png'))
before = Image.open('Mario.png')
after=before.filter(ImageFilter.BLUR)
after.save("MarioBLUR.png")

我得到的错误是:

Traceback(最近一次调用最后一次): 文件“C:\Users\User\eclipse-workspace\Practice Python CS50 2019\src\Class 6\blur.py”,第 5 行,在 之前 = Image.open('Mario.png') 文件“C:\Users\User\anaconda3\lib\site-packages\PIL\Image.py”,第 2809 行,打开 fp = builtins.open(文件名,“rb”) FileNotFoundError:[Errno 2] 没有这样的文件或目录:'Mario.png'

这张图片我的windows位置是:C:\Users\User\Downloads\Mario.png 我的日食位置是:C:\Users\User\eclipse-workspace\Practice Python\images\Mario.png

如何将此图片添加到正确的目录以确保不再出现此问题?

【问题讨论】:

    标签: python-3.x eclipse


    【解决方案1】:

    你只需要os.path.dirname中的目录路径而不需要文件名,例如:

    root_dir= os.path.dirname('C:/Users/User/eclipse-workspace/Practice Python CS50 2019/images/')
    before = Image.open(root_dir + 'Mario.png')
    

    应该没问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2015-11-19
      • 2013-03-21
      • 2015-04-09
      • 2019-05-26
      相关资源
      最近更新 更多