Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with windows paths
#1
Hello,
i try to create a button with an image. Problem: The correct Path is not interpreted correctly.
When i try this code
        image_path = os.path.join(self.cwd, "OpenFolder.png")
        print(image_path)
        open_button = Button(frame1, image=image_path)
with "print(image_path) it is printing correctly (Windows):
Output:
C:\SVN\Python\Trunk\Projects\PhotoViewer\OpenFolder.png
but gives the exception:
Error:
_tkinter.TclError: image "C:SVNPythonTrunkProjectsPhotoViewerOpenFolder.png" doesn't exist
So somehow the slashes are removed or ignored.

Generally:
How to get, join and retreive pathes correctly, if possible, platform independent?
I tried with os.getcwd() and os.path.join but somehow there's always a problem...
Reply
#2
The backslash is how you specify special control characters (\n is newline, \t is tab). You can use '\\' to get a backslash character. You can use a raw string to treat all characters as normal characters, r'C:\...'. You can use forward slashes.
Reply
#3
Sorry, but this is noting new for me.
But when i get the path from the system using the python os class it should already be int the correct format.
Note that i didn't specify the path by myself like path = "C:\svn\..." in this case it would be a string with the special characters \ for python.
And this has nothing to do that it is once with the slashes and later without.?
In my opinion the os class should be capable to manage this problems.
Note that i already used
self.cwd = os.getcwd
to get this path...
Reply
#4
All I know is that when I see examples using images in tkinter the filenames either use forward slashes, double backslashes or raw strings. Another solution (better solution) is use the pathlib.Path() function.

As for you opinions, I don't think the os or python cares about your opinions. They certainly don't care about mine.
Reply
#5
Now i changed the pathname by hand using
image_path = image_path.replace("\\", "/")
and i got the error
Error:
_tkinter.TclError: image "C:/SVN/Python/Trunk/Projects/PhotoViewer/OpenFolder.png" doesn't exist
but when i copy the exact string into windows file manager it opens the file without problem. So the path is correct..
I think that this is just a wrong error message...
Reply
#6
"self.cwd = os.getcwd" sets self.cwd equal to the function os.getcwd. When I add parens to evaluate the function I get a path that does not end with '/'. For example I get 'C:\Users\me\Musings' which does not result in a valid filename when I add 'image.png'. So this is an error:
from PIL import Image
import os
f = Image.open(os.getcwd() + 'Image.png')
Output:
Traceback (most recent call last): File "C:\Users\me\Musings\junk.py", line 3, in <module> f = Image.open(os.getcwd() + 'Image.png') File "C:\Program Files\Python38\lib\site-packages\PIL\Image.py", line 2809, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\me\\MusingsImage.png'
No tailing '\' in the cwd path, so no '\\' in the file it tries to open. But this program that uses pathlib.Path works fine.
from pathlib import Path
from PIL import Image
f = Image.open(Path.cwd() / 'Image.png')
f.show()
However when I use the same idea in tkinter I get the same error you are seeing.
from tkinter import *
from pathlib import Path

root = Tk()
image_file = Path.cwd() / 'image.png'
print(image_file)
open_button = Button(root, image=image_file)
Output:
C:\Users\me\musings\image.png Traceback (most recent call last): File "C:\Users\me\musings\junk.py", line 7, in <module> open_button = Button(root, image=image_file) File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 2645, in __init__ Widget.__init__(self, master, 'button', cnf, kw) File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 2567, in __init__ self.tk.call( _tkinter.TclError: image "C:\Users\me\musings\image.png" doesn't exist
This is when I finally realize I had blindly followed you down the rabit hole and the button image argument is expecting an image object, not a filename. Duh!!! Helps to read the documentation instead of banging away at the keyboard sometimes.
from tkinter import *
from pathlib import Path

root = Tk()
image_file = Path.cwd() / 'image.png'
image = PhotoImage(file = image_file)
open_button = Button(root, image=image)
When debugging it is always a good idea to debug the real problem and not what we think is the problem. The first and often the hardest step.
Reply
#7
delphinis Wrote:_tkinter.TclError
Observe that the error doesn't come from Python, it comes from the Tcl/Tk library upon which tkinter is sitting. This library may well accept only valid OS paths.

I'm not in the best position to answer, since I think a very good solution would be to get rid of the Windows OS...

More seriously, using the pathlib library to manipulate paths is a robust solution. Starting with Python 3.6, you can also use os.fspath, for example
import os
from pathlib import Path
filename = Path.cwd() / 'spam.png'
astring = os.fspath(filename) # see if this returns a valid Windows path
In good old python 1, 2, 3, one would have written
import os
astring = os.path.join(os.getcwd(), 'spam.png')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pdf2image, poppler and paths jehoshua 18 13,942 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,416 May-29-2022, 09:11 AM
Last Post: snippsat
  automatically get absolute paths oclmedyb 1 2,061 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  chkFile with absolute paths JarredAwesome 7 2,912 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  Paths millpond 12 5,080 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Shortest paths to win snake and ladder sandaab 5 4,178 Jun-30-2019, 03:20 PM
Last Post: sandaab
  How to handle paths with spaces in the name? zBernie 1 6,666 Nov-22-2018, 04:04 AM
Last Post: ichabod801
  Question: Paths and writing to a file mwmaw 6 6,380 Dec-20-2016, 03:44 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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