Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Best way to reference file one level up in directory?

Tags:

python

I'm a Python newbie and I'm running into a problem that I feel like should have a relatively straight forward answer that I can't quite figure out.

I need to do it this way for an assignment as our teacher wants our GitHub folder to contain all the code in one folder and all of the images in another folder.

Since I will be in the Code folder when running the Python file, is there a way I can reference the Image Folder? Locally, I know I can just put the full path, but because this will be done through command line for GitHub, I wasn't sure what the full path even looked like to reference that folder.

+ Main GitHub Directory
    + Code Folder
       -My Python File
    + Image Folder
       -Images I want to use

This is just a sample program of what I would like to do:

from PIL import Image
image = Image.open('WeatherIcons/Code32.PNG')
image.show()

This will work if 'WeatherIcons' is a subfolder of the Code Folder, but not when it is at the same level. Any creative solutions to get around this?

Thank you!

like image 737
user6142489 Avatar asked Oct 17 '25 12:10

user6142489


1 Answers

Use the path:

../Image_Folder/Code32.png
like image 180
TVASO Avatar answered Oct 20 '25 02:10

TVASO