Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不存在文件或目录,但确实存在_Python_File_Tkinter - Fatal编程技术网

Python 不存在文件或目录,但确实存在

Python 不存在文件或目录,但确实存在,python,file,tkinter,Python,File,Tkinter,我正在尝试使用tkinter模块打开一个窗口以显示图片,之前遇到以下问题: 此后,我使用以下问题将我的代码重组为: 在使用Python3时,我对代码做了一些更改。 当我试图执行代码时:我得到一个错误,说没有文件或目录存在。但是,我已使用以下代码进行检查: 以下是检查文件的结果: >>> os.listdir() ['comments.py', 'Conan.txt', 'conditions.py', 'dateandtime.py', 'desktop.ini', 'dic

我正在尝试使用tkinter模块打开一个窗口以显示图片,之前遇到以下问题: 此后,我使用以下问题将我的代码重组为: 在使用Python3时,我对代码做了一些更改。 当我试图执行代码时:我得到一个错误,说没有文件或目录存在。但是,我已使用以下代码进行检查: 以下是检查文件的结果:

 >>> os.listdir()
['comments.py', 'Conan.txt', 'conditions.py', 'dateandtime.py', 'desktop.ini', 'dictionaries.py', 'exceptions.py', 'forging_functions.py', 'formatting.py', 'graphics.py', 'Hello.py', 'leapdays.py', 'logging.py', 'loop.py', 'modules.py', 'months.py', 'numbers.py.py', 'Opening_files.py', 'Picture.jpg', 'print_hello.py.py', 'tkintercode1.py', 'tkintercode2.py', 'user_input.py', 'Writing_file.py', '__pycache__']
  >>> os.getcwd()
'C:\\Users\\Draco\\OneDrive\\Documents\\Programming'
 >>> os.chdir(r'C:\\Users\\Draco\\OneDrive\\Documents\\Programming')
 >>> open('Picture.jpg')
 <_io.TextIOWrapper name='Picture.jpg' mode='r' encoding='cp1252'>
非常感谢您的帮助

“C:\Draco\OneDrive\Documents\Programming\Picture.jpg”

试试
path=“C:\\Draco\\OneDrive\\Documents\\Programming\\Picture.jpg”
-双斜杠
编辑

你有不同的路径。Users\Draco但在代码just Draco中

请显示准确的错误回溯(最近一次调用):文件“C:\Users\Draco\OneDrive\Documents\Programming\tkintercode2.py”,第12行,在img=open(path)FileNotFoundError:[Errno 2]没有这样的文件或目录:“C:\\Draco\\OneDrive\\Documents\\Programming\\Picture.jpg”这是我遇到的错误,但我已经检查了python是否识别了路径,看起来是这样的,但是当我执行它时,我得到了上面的错误消息,我也去掉了tkinter.PhotoImage(Image)。并让那一段代码作为path=open(path)我已经尝试过了,但不幸的是,它仍然有效,仍然显示相同的错误消息
import tkinter as tk
from tkinter import *

window = tk.Tk()
window.title("Random Image")
window.geometry("300x300")
window.configure(background='grey')

path = "C:\Draco\OneDrive\Documents\Programming\Picture.jpg" 


img = tkinter.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)

panel.pack(side = "bottom", fill = "both", expand = "yes")

window.mainloop()