将每个导出的文件放在自己的文件夹中

时间:2018-09-27 09:04:20

标签: python pandas

我正在导出一些文件,我想将其导出到“导出”文件夹中。

文件为file1.shp,file2.shp,etc

我想用相同的名称将它们保存在自己的文件夹中。 像:exports/file1/file1.shp等等。

直到现在,我已经设法使其以这种方式保存:

exports / file1.shp以及file2.shp等。所有内容都分散在exports中,简单来说。

我该如何像第一个示例一样?

代码:

folder = path.Path(path)
d = defaultdict(list)
os.mkdir(folder+'\\'+ 'exports' ) 

with open(folder+ '\\'+'exports'+'\\'+'log.txt', 'w') as fw:
    for shpfile  in glob.iglob(folder + '/*/*/*.shp' ,recursive = True):
        try:
            df = geopandas.read_file(shpfile)
            d[os.path.basename(shpfile)].append(df)
        except FionaValueError as ex:
            if not os.listdir(shpfile): 
                fw.write(f'{shpfile} is empty' + '\n')
    for cat, lst in d.items():
        if cat.endswith('.shp'): 
            pd.concat(lst,sort = True).to_file(folder/'exports'/cat)

0 个答案:

没有答案