简体   繁体   English

FileNotFoundError:[Errno 2] Python 3.7 上的图片没有此类文件或目录

[英]FileNotFoundError: [Errno 2] No such file or directory for pictures on Python 3.7

I am a beginner on Python.我是 Python 的初学者。 I try to open a picture to print its size and format.我尝试打开一张图片来打印它的大小和格式。 My picture is in several folders:我的图片在几个文件夹中:

  • my images我的图片
  • the folder of my Python codes我的 Python 代码的文件夹
  • the folder of anaconda3\lib\site-packages\PIL anaconda3\lib\site-packages\PIL的文件夹

I have already wrote only the name of the picture, then the exact path but nothing works (my picture is in .jpg ).我已经只写了图片的名称,然后是确切的路径,但没有任何效果(我的图片在.jpg中)。

Here is my code:这是我的代码:

from PIL import Image

image = Image.open("v")    
print("nbr of columns and lines", image.size, image.format)
data = list(image.getdata())
print(data[:30]) 

and my error message:和我的错误信息:

runfile('C:/Users/Optimal Conseil/.spyder-py3/temp.py', wdir='C:/Users/Optimal Conseil/.spyder-py3') Traceback (most recent call last): runfile('C:/Users/Optimal Conseil/.spyder-py3/temp.py', wdir='C:/Users/Optimal Conseil/.spyder-py3') Traceback(最近一次调用最后):

File "C:\Users\Optimal Conseil.spyder-py3\temp.py", line 4, in image=Image.open("v")文件“C:\Users\Optimal Conseil.spyder-py3\temp.py”,第 4 行,在 image=Image.open("v")

File "C:\Users\Optimal Conseil\anaconda3\lib\site-packages\PIL\Image.py", line 2809, in open fp = builtins.open(filename, "rb")文件“C:\Users\Optimal Conseil\anaconda3\lib\site-packages\PIL\Image.py”,第 2809 行,打开 fp = builtins.open(filename, "rb")

FileNotFoundError: [Errno 2] No such file or directory: 'v' FileNotFoundError:[Errno 2] 没有这样的文件或目录:'v'

If the image file "v" is in the same folder as your python code, your code should load it.如果图像文件“v”与您的 python 代码位于同一文件夹中,则您的代码应该加载它。 The error shows it is missing the file.该错误表明它缺少该文件。 I would recommend checking for file extensions (maybe file name is "v.jpg").我建议检查文件扩展名(可能文件名是“v.jpg”)。
And if your file is not in the python folder and is in a different folder, the easiest way is to include the full address to your image file:如果您的文件不在 python 文件夹中并且在不同的文件夹中,最简单的方法是将完整地址包含到您的图像文件中:

image=Image.open("full_path_to_file/"+"v")

The same approach with better practice would be like this:具有更好实践的相同方法将是这样的:

import os
image=Image.open(os.path.join("full_path_to_file", "v"))

You can also use the relational address to file.您也可以使用关系地址来归档。 "../" will take you to one directory above your current directory. “../”将带您到当前目录上方的一个目录。 For example, if your file directory structure is:例如,如果您的文件目录结构是:

  • directory目录
    • | |
    • v.jpg v.jpg
    • subdirectory子目录
      • | |
      • your_python_code.py your_python_code.py

You can use:您可以使用:

image=Image.open("../v")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 FileNotFoundError: [Errno 2] Python 3.7/Mac 没有这样的文件或目录 - How to solve FileNotFoundError: [Errno 2] No such file or directory for Python 3.7/Mac python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录 [Python] - FileNotFoundError: [Errno 2] No such file or directory [Python] Python 3-FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3 - FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError: [Errno 2] 没有这样的文件或目录 Azure Python 错误 - FileNotFoundError: [Errno 2] No such file or directory Azure Python error Python 错误:FileNotFoundError: [Errno 2] 没有那个文件或目录 - Python error: FileNotFoundError: [Errno 2] No such file or directory Python 错误 FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python error FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录 (Python 3.10) - FileNotFoundError: [Errno 2] No such file or directory (Python 3.10)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM