Python Forum
Problem with absolute path to file (Mr.Stickman Problems)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with absolute path to file (Mr.Stickman Problems)
#1
Right now I'm working on getting the backround. So I ran into a problem and someone said to provide the absolute value of the file of the picture so I did that but it still doesnt work can someone please tell me whats wrong with it.
from tkinter import *
import random
import time
import os

class Game:
    def __init__(self):
        self.tk = Tk()
        self.tk.title("Mr.Stickman Races for the Exit")
        self.tk.resizable(0, 0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=500, height=500, highlightthicknes=0)
        self.canvas.pack()
        self.tk.update()
        self.canvas_height = 500
        self.canvas_width = 500
        self.bg = PhotoImage(file=os.path.abspath('Background.gif.xcf'))
        w = self.bg.width()
        h = self.gb.height()
        for x in range(0, 5):
            for y in range(0, 5):
                self.canvas.create_image_(x * w, y * h, image=self.bg, anchor='nw')
        self.sprites = []
        self.running = True

    def mainloop(self):
        while 1:
            if self.running == True:
                for sprite in self.sprites:
                    sprite.move()
            self.tk.update_idletasks()
            self.tk.update()
            time.sleepp(0.01)

g = Game()
g.mainloop
            
Error:
Traceback (most recent call last): File "C:\Users\sheep\Desktop\Ayden's Apps\Mr.Stick Man.py", line 35, in <module> g = Game() File "C:\Users\sheep\Desktop\Ayden's Apps\Mr.Stick Man.py", line 17, in __init__ self.bg = PhotoImage(file=os.path.abspath('Background.gif.xcf')) File "C:\Users\sheep\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3539, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\sheep\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3495, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "C:\Users\sheep\Desktop\Ayden's Apps\Background.gif.xcf": no such file or directory >>>
Reply
#2
This s pretty self-explanatory

Quote:_tkinter.TclError: couldn't open "C:\Users\sheep\Desktop\Ayden's Apps\Background.gif.xcf": no such file or directory

Python will use a forward slash on any OS so try

Quote:"C:/Users/sheep/Desktop/Ayden's Apps/Background.gif.xcf"
Reply
#3
@woooee - although you are 100% correct and one should use forward slash or raw string when provide path as string, in this case the path is created with this code os.path.abspath('Background.gif.xcf') and thus it is not backslash problem.

@SheeppOSU: Now if you look at the docs, you would see that os.path.abspath(path) is equivalent (on most systems) to normpath(join(os.getcwd(), path)). Given that OP provides just a file name, it will construct absolute path based on current working directory. I would guess that the file is indeed NOT in the current working directory. In other words it is a problem due to OP misunderstanding how os.path.abspath(path) works.

You should provide absolute path as a string(using forward slash)/raw string or use proper python tools to construct the correct path.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Sometimes when you start a program from another directory, or call a subprogram, the working directory mat not be what you thing it is.
This can be a problem is you assume it starts with the python file directory.
You can assure a starting location with the following snippet:
import os

path = os.path.dirname(__file__)
os.chdir(path)
you can then get the absolute path to your 'Background.gif.xcf' (is that really the file name?) file by using a relative
path as argument to os.path.abspath(relative_path) (of course replacing relative path with something like 'images/Background.gif.xcf')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add file to sys.path permanently hcccs 5 282 Jan-31-2021, 11:26 AM
Last Post: hcccs
  Problems Sorting Data in an External File (.txt) Superlegend21 1 225 Dec-27-2020, 10:06 PM
Last Post: Superlegend21
  Absolute beginner am I missing something? kamren 7 477 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 399 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  problem about maze path finder Simurg 2 393 Aug-16-2020, 01:10 PM
Last Post: Simurg
  absolute imports between folders mikisDW 0 290 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  Problems on ''no such file or directory'' in python on Visual studio 2019 504416430 1 809 Aug-02-2020, 08:38 PM
Last Post: Larz60+
  PyDrive download file path MiniMinnow 0 992 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  I am an absolute newbie.Help me! feynarun 4 564 Apr-11-2020, 09:25 PM
Last Post: feynarun
  String to File Path creedX 4 685 Apr-06-2020, 07:29 PM
Last Post: creedX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020