繁体   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