Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Access a image from another directory? python

Tags:

python

I have a python script say "foo_1.py" which resides in folder x and another script foo_2.py which resides in folder y.

Folders x and y are in folder z.

I called a method which belongs to foo_2.py which generates an image in folder y (say img1.png). Now what I want is to access this img1.png from foo_1.py. How is it possible?

like image 342
Ria Avatar asked Oct 22 '25 13:10

Ria


1 Answers

sys.path[0] gives you the directory where the current script resides:

import sys, os
script_dir = sys.path[0]
img_path = os.path.join(script_dir, '../y/img1.png')
like image 194
CharlesB Avatar answered Oct 24 '25 01:10

CharlesB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!