簡體   English   中英

如何從另一個目錄訪問圖像? 蟒蛇

[英]How to Access a image from another directory? python

我有一個python腳本說“foo_1.py”位於文件夾x中,另一個腳本foo_2.py位於文件夾y中。

文件夾x和y位於文件夾z中。

我調用了一個屬於foo_2.py的方法,它在文件夾y中生成一個圖像(比如img1.png)。 現在我想要的是從foo_1.py訪問這個img1.png。 這怎么可能?

sys.path[0]為您提供當前腳本所在的目錄:

import sys, os
script_dir = sys.path[0]
img_path = os.path.join(script_dir, '../y/img1.png')
img_file = open('../y/img1.png')

這與Python有什么關系? 使用已知的“..”表示法使用絕對路徑或相對路徑。 這是Shell的基本知識。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM