簡體   English   中英

Python | os.path.join() 方法

[英]Python | os.path.join() method

我需要有關在 GitHub 中找到的以下代碼行的幫助: 土地利用土地覆蓋

DATA_FOLDER = os.path.join('..', '..', 'example_data')

我不知道在 '.' 之間放什么,而且我只下載了 svn_buffered.geojson 文件。 這是它的當前目錄:C:\Users\ASUS\Desktop\PFE-Master\Code 我不明白為什么我需要連接多個路徑。

這是完整的代碼:

# Folder where data for running the notebook is stored
DATA_FOLDER = os.path.join('..', '..', 'example_data')

# Load geojson file
country = gpd.read_file(os.path.join(DATA_FOLDER, 'svn_buffered.geojson'))

# Convert CRS to UTM_33N
country_crs = CRS.UTM_33N
country = country.to_crs(crs=country_crs.pyproj_crs())

# Get the country's shape in polygon format
country_shape = country.geometry.tolist()[-1]

# Plot country
country.plot()
plt.axis('off');

# Print size 
print('Dimension of the area is {0:.0f} x {1:.0f} m2'.format(country_shape.bounds[2] - country_shape.bounds[0],
                                                             country_shape.bounds[3] - country_shape.bounds[1]))```

您鏈接的腳本是SI_LULC_pipeline.ipynb ,它位於eo-learn/examples/land-cover-map/SI_LULC_pipeline.ipynb的項目中。

由於它試圖訪問eo-learn/example_data/中的數據,要從腳本的工作目錄(顯然是它所在的文件夾)獲取數據,它需要訪問../../example_data在世界上幾乎所有的操作系統和 web 或 Windows 上的..\..\example_data上。

為了避免使用一種或另一種操作系統約定,腳本的作者保持它干凈並改為調用os.path.join('..', '..', 'example_data') ,這將它留給 Python 到決定是否使用'/''\'來分隔路徑的各個部分(或運行它的操作系統上的任何符號)。

如果'..'本身讓您感到困惑: ..表示“當前目錄的父目錄”。 任何路徑要么從根目錄開始(如果它以\/開始),要么從腳本的當前工作目錄開始。 要通過父目錄訪問相對於當前工作目錄的目錄,請使用.. 同樣, . 指當前工作目錄,您可以在路徑的開頭使用它來使其明確地相對於工作目錄。

請注意https://github.com/sentinel-hub/eo-learn/blob/master/example_data/svn_buffered.geojson如何位於master/example_data/ - 將其與腳本本身位於master/examples/land-cover-map land 的路徑進行比較master/examples/land-cover-map 要從腳本獲取數據,您需要 go 到父文件夾,然后再次到父文件夾,然后進入example_data

暫無
暫無

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

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