How to Access a image from another directory? 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 cal开发者_开发百科led 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?
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')
img_file = open('../y/img1.png')
What has this to do with Python? Either use an absolute path or a relative path using the wellknown ".." notation. This is Shell basic knowhow.
精彩评论