Python Forum
Problems with windows paths - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problems with windows paths (/thread-28496.html)



Problems with windows paths - delphinis - Jul-21-2020

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...


RE: Problems with windows paths - deanhystad - Jul-21-2020

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.


RE: Problems with windows paths - delphinis - Jul-21-2020

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...


RE: Problems with windows paths - deanhystad - Jul-21-2020

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.


RE: Problems with windows paths - delphinis - Jul-21-2020

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...


RE: Problems with windows paths - deanhystad - Jul-21-2020

"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.


RE: Problems with windows paths - Gribouillis - Jul-21-2020

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')