没有这样的文件或目录

问题描述 投票:0回答:3

我基本上是在尝试制作一个游戏,用户可以猜测美国某个州的名称,如果存在,该名称将写在该州的轮廓上。这是我想做的游戏:https://www.sporcle.com/games/g/states

import turtle
import pandas as pd

import os
os.system('cls' if os.name == 'nt' else 'clear')

image_path = "D:\python course\day 25 - introduction to pandas\guess the states game\blank_states_img.png"
data_file_path = "D:\python course\day 25 - introduction to pandas\guess the states game\50_states.csv"

screen = turtle.Screen()
screen.title("U.S. States Game")
image = image_path
screen.bgpic(image_path)

data = pd.read_csv(data_file_path)

states_data = data["state"]

game_is_on = True

while game_is_on:
    answer = screen.textinput(title="Guess a state",
                              prompt="Write name of a state").capitalize()

    if answer in states_data:
        state_details = data[data.state == answer]
        text = turtle.Turtle()
        text.hideturtle()
        text.pu()
        text.goto(state_details[1], state_details[2])
        text.write(answer)

screen.mainloop()

错误:

self._bgpics[picname] = self._image(picname)
                            ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 479, in _image
    return TK.PhotoImage(file=filename, master=self.cv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 4120, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 4065, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "D:\python course\day 25 - introduction to pandas\guess the states gamlank_states_img.png": no such
file or directory

我还尝试了屏幕的 addshape 方法,但不起作用。 上面使用的路径是直接复制的,所以不存在排版问题。但我看到了这个错误。

python turtle-graphics python-turtle
3个回答
0
投票

当使用 Windows 样式路径名(即,使用 \ 作为目录分隔符)时,必须将此类字符串指定为 raw。这是因为反斜杠可能位于一个字符之前,该字符会使该对成为众所周知的转义字符,例如 a、b、n、t、r、f、v

在问题中我们有:

image_path = "D:\python course\day 25 - introduction to pandas\guess the states game\blank_states_img.png"

...其中包含,因此该字符串被解释为:

D:\python 课程\第 25 天 - pandas 简介\猜测状态 gamlank_states_img.png

要克服这个问题,请使用原始字符串前缀,如下所示:

image_path = r"D:\python 课程\第 25 天 - pandas 简介\猜国家游戏 lank_states_img.png"


0
投票

很可能您输入的 gif 文件名错误。也许它是blank_states_img.gif而不是blank_state_img.gif(你忘记了's') 因此,请检查复数和拼写错误。祝你好运! :)


0
投票
I had the same error but I sorted out by going back a directory in filepath. I was using relative filepath.
Absolute filepath: C:\LocalDiskA\IT\Python\Project\day25\50states\blank_states_img.gif
main.py filepath :C:\LocalDiskA\IT\Python\Project\day25\50states\main.py
Expected relative filepath(which is giving error): blank_states_img.gif
Working relative filepath: day25\50states\blank_states_img.gif
© www.soinside.com 2019 - 2024. All rights reserved.