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): but gives the exception: 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.getcwdto 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 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') 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) 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.TclErrorObserve 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 pathIn good old python 1, 2, 3, one would have written import os astring = os.path.join(os.getcwd(), 'spam.png') |