0

I'm trying to set a path to an icon in my project.

"/icons/currency/USD.png"

But I'm getting an error:

FileNotFoundError: [Errno 2] No such file or directory: 'icons/currency/USD.png'

If I set the path to "/icons/USD.png" everything is work. but I don't want all my icons will be in one folder so I open currency folder to separate it from other icons.

my project folders structure is:

project folder -> name_of_project.py , icons folder

icons folder -> currency folder -> USD.png

What is the correct way to configure this relative path ?

3
  • 1
    Show us the code that uses the icon path! Commented Jun 29, 2020 at 9:43
  • / is referring to the root (Linux), root of a drive (Windows) or root of webserver. If /icons/USD.png works, that means a) you have not moved the file into the subfolder (for local files) or b) you have some caching issues (for Webserver). Please give more information about your OS and the project type (web, desktop or console). Commented Jun 29, 2020 at 9:48
  • not works: img_dollar_flag = Image.open("/icons/currency/USD.png") img_dollar_flag = img_dollar_flag.resize((30, 30), Image.ANTIALIAS) img_dollar_flag = ImageTk.PhotoImage(img_dollar_flag) this works: img_dollar_flag = Image.open("/icons/ USD.png") img_dollar_flag = img_dollar_flag.resize((30, 30), Image.ANTIALIAS) img_dollar_flag = ImageTk.PhotoImage(img_dollar_flag) Commented Jun 29, 2020 at 10:26

1 Answer 1

1

I'd recommend using os.path.join() to get full paths without bothering with your OS's specifics.

Currently you are setting your path to start from the root of your OS (I'm assuming Linux because of the path you have given as example). What you need is to get the path (absolute) of the file you are running and navigating from there.

You can do it as follows:

abs_path_of_executable_file = os.path.split(os.path.abspath(__file__))[0]

path_to_png = os.path.join(abs_path_of_executable_file, 'icons', 'currency', 'USD.png')

# do stuff with path_to_png

Where the first line gets the absolute path of the file you are executing (os.path.abspath(__file__)) and then you are taking the full path of the directory containing that file (os.path.split('some/path/to/file.py')[0]). os.path.split('some/path/to/file.py')[1] gives you only the filename, without any paths.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using Windows. I'm making some gui using tkinter. What do you mean that I'm setting my path to start from the root of my OS ? when I checked this line: os.path.abspath(file) it's returned the location of my project file path.'C:\\Users\\eliran\\Desktop\\Project\\project.py' sorry I'm not so advance in Python to understand all you wrote
after running \ playing with the code lines you provide me I understand it now !!!! thanks a lot. This line: (os.path.abspath(file))[0] will always return the path of my project file?
Great, so os.path.abspath() behaves exactly as expected. In this case, if you execute the line os.path.split('C:\\Users\\eliran\\Desktop\\Project\\project.py')[0] you will get 'C:\\Users\\eliran\\Desktop\\Project'. From this point, you can navigate to any directory in 'Project'. For example, to go to a picture called "USD.png" in "icons/currency" you would write: os.path.join('C:\\Users\\eliran\\Desktop\\Project', 'icons', 'currency', 'USD.png') and should get "C:\\Users\\eliran\\Desktop\\Project\\icons\\currency\\USD.png"
Regarding your second comment: os.path.abspath(file)[0] will return the full path to the directory where the file that is currently executed is located. So if your file is called project.py, then yes - it will always return the full path to this file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.