将txt元素连接到Python中提到的文件

2024-09-24 22:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有50行命令的文本文件。请看下面这些代表什么

我想要一个主文件夹的目录,其中包含子文件夹,每个子文件夹包含ASTIK.shpASTIK子文件夹中,然后是EAS.shp,在EAS子文件夹中,然后是ASTOM.shp,等等

INT表示与此代码执行交集,在这种情况下,应该涉及ASTIKEAS形状文件

例如,行:

INT_ASTIK_EAS  implies:

import geopandas
inte_s=gpd.overlay(ASTIK,EAS,how='intersection')

然后转到另一行并“理解”该行的含义,并在两个命名的shapefile之间执行擦除(差异)

如果文件名前面有ER,例如:

ER_ASTOM_ASTIK

它应该执行:

 er=gpd.overlay(ASTOM,ASTIK,how='difference')

然后找到确切的文件并执行它

这里是从文本中读取文件并逐行执行实际操作的自动化

action_dict = {'INT': 'intersection', 'ER': 'difference'}#associate the 'INT' with the intersection operation and so on
directory=input('Insert dir of the main folder')
with open(input()) as txtfile: #insert directory of txt
    x = txtfile.readlines()
for line in x:
    action, shape1, shape2 = line.split('_')
    print( action, shape1, shape2) # here line is ER_ASTOM_ASTIK or whatever line in the txt file
    if shape1 in sfiles and shape2 in sfiles:
        gpd.overlay(sfiles[shape1], sfiles[shape2], how=action_dict[action])

我正在考虑遍历主文件夹并捕获这些文件,但是有更好的方法吗?我该如何连接


Tags: 文件thein文件夹lineactioninteas