no such file or directory

Advertisements

I am basically trying to make a game where the user can guess the name of an american state, and if it exists, the name will get written on the outline of that state. This is the game I am trying to make: 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()

Error:

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

I also tried addshape method for screen, but not working.
The paths used above are directly copied, so there is no typographical issue. Yet I am seeing this error.

>Solution :

When using Windows style pathnames (i.e., with \ as directory separator) it’s import to specify such strings as raw. This is because a backslash might precede a character that would make the pair into a well-known escape character e.g., a, b, n, t, r, f, v

In the question we have:

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

…which contains \b and so the string is interpreted as:

D:\python course\day 25 – introduction to pandas\guess the states gamlank_states_img.png

To overcome this use a raw string prefix as follows:

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

Leave a ReplyCancel reply