-2

I had made a program where I require files from that directory where the python file is located. The files are located in the same folder where the python file located.

I can't write the full path of the file in the python script I used the ./ method in the script but it gives error

Please help me to solve the problem

here is my code

'''

#code

root=Tk()
root.title("SOHAM YOUTUBE VIDEO DOWNLOADER")
root["bg"]='#1F1F1F'
root.iconbitmap("./img/youtube_(1).ico")
root.geometry("900x680")

file =PhotoImage(file="./img/youtube-icon.png")
headingIcon=Label(root, image=file)
headingIcon.pack(side=TOP, pady=3)

'''

My folder location

here img is my required folder in YOUTUBE_VEDIO_DOWNLOADER.py which is my python file

Here img is my required folder in YOUTUBE_VIDEO_DOWNLOADER.py which is my python file

My script error is given below

y"python "e:/python projects/YOUTUBE VIDEO DOWNLOADER/YOUTUBE_VIDEO_DOWNLOADER.py
Traceback (most recent call last):*emphasized text*
  File "e:\python projects\YOUTUBE VIDEO DOWNLOADER\YOUTUBE_VIDEO_DOWNLOADER.py", line 215, in <module>
    root.iconbitmap("./img/youtube_(1).ico")
  File "C:\Users\soham\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2073, in wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "./img/youtube_(1).ico" not defined

1 Answers1

2

One trick I use to account for this problem, is to use the __file__ variable (which holds the path of the scripts file) as my reference path and then write the path to the file I want.

If your directory looks like this:

my_dir
|> my_script.py
|> img/
   |> my_img.png

Then __file__ will be equal to C:\\...\my_dir\my_script.py.

And your image will be os.path.join(os.path.dirname(__file__), 'img', 'my_img.png')

ygorg
  • 750
  • 3
  • 11
  • Sir I had tried it but it not working icopath = os.path.join(os.path.dirname(__file__), 'img', 'youtube_(1)') root.iconbitmap(icopath) – SOHAM DAS BISWAS. Oct 22 '20 at 14:35
  • Error $ python "e:/python projects/YOUTUBE VIDEO DOWNLOADER/YOUTUBE_VIDEO_DOWNLOADER.py" Traceback (most recent call last): File "e:\python projects\YOUTUBE VIDEO DOWNLOADER\YOUTUBE_VIDEO_DOWNLOADER.py", line 221, in root.iconbitmap(icopath) File "C:\Users\soham\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2073, in wm_iconbitmap return self.tk.call('wm', 'iconbitmap', self._w, bitmap) _tkinter.TclError: bitmap "e:\python projects\YOUTUBE VIDEO DOWNLOADER\img\youtube_(1)" not defined – SOHAM DAS BISWAS. Oct 22 '20 at 14:36
  • I think you forgot the `.ico` in the image name – ygorg Oct 22 '20 at 14:40
  • no sir I had wrote .ico file and it start working when I gave the full path of the file but I require to give it a short path like "./img/youtube_(1).ico" – SOHAM DAS BISWAS. Oct 22 '20 at 14:45
  • 1
    I think you can print the full path you made and the path that uses `__file__`. Compare them and adjust the code accordingly. – ygorg Oct 22 '20 at 14:47
  • when I print the path it gives "e:\python projects\YOUTUBE VIDEO DOWNLOADER\YOUTUBE_VIDEO_DOWNLOADER.py" – SOHAM DAS BISWAS. Oct 22 '20 at 14:50
  • Sir I have to replace "YOUTUBE_VIDEO_DOWNLOADER.py" to "img\youtube_(1)" – SOHAM DAS BISWAS. Oct 22 '20 at 14:56
  • Sir I had tried a code "root.iconbitmap(__file__+"/../img/youtube_(1).ico")" and it worked – SOHAM DAS BISWAS. Oct 22 '20 at 15:02
  • Thank you Sir, for help me and spend your precious time – SOHAM DAS BISWAS. Oct 22 '20 at 15:03