Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with windows paths
#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


Messages In This Thread
Problems with windows paths - by delphinis - Jul-21-2020, 11:03 AM
RE: Problems with windows paths - by deanhystad - Jul-21-2020, 02:38 PM
RE: Problems with windows paths - by delphinis - Jul-21-2020, 03:17 PM
RE: Problems with windows paths - by deanhystad - Jul-21-2020, 03:24 PM
RE: Problems with windows paths - by delphinis - Jul-21-2020, 04:17 PM
RE: Problems with windows paths - by deanhystad - Jul-21-2020, 05:56 PM
RE: Problems with windows paths - by Gribouillis - Jul-21-2020, 06:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pdf2image, poppler and paths jehoshua 18 15,175 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,527 May-29-2022, 09:11 AM
Last Post: snippsat
  automatically get absolute paths oclmedyb 1 2,147 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  chkFile with absolute paths JarredAwesome 7 3,057 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  Paths millpond 12 5,339 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Shortest paths to win snake and ladder sandaab 5 4,326 Jun-30-2019, 03:20 PM
Last Post: sandaab
  How to handle paths with spaces in the name? zBernie 1 6,795 Nov-22-2018, 04:04 AM
Last Post: ichabod801
  Question: Paths and writing to a file mwmaw 6 6,610 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