import numpy as np import os from glob import glob from pathlib import Path # from multiprocessing import Pool ######################################################## # 函数区 ######################################################## spacetySliceEnvPathExEPath=r"d:\ProgramData\anaconda3\envs\spacetySliceEnv\python.exe" def find_tif_files_pathlib(directory): """ 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 参数: directory (str): 要搜索的根目录路径 返回: list: 包含所有找到的.tif/.tiff文件完整路径的列表 """ path = Path(directory) # 使用rglob递归匹配所有.tif和.tiff文件 tif_files = list(path.rglob('*.tif')) + list(path.rglob('*.tiff')) # 将Path对象转换为字符串路径 return [str(file) for file in tif_files] def find_shp_files_pathlib(directory): """ 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 参数: directory (str): 要搜索的根目录路径 返回: list: 包含所有找到的.tif/.tiff文件完整路径的列表 """ path = Path(directory) # 使用rglob递归匹配所有.tif和.tiff文件 tif_files = list(path.rglob('*.shp')) # 将Path对象转换为字符串路径 return [str(file) for file in tif_files] def preProcessLabelshapefile(shpfilepath,preFolderPath): if not os.path.exists(preFolderPath): os.makedirs(preFolderPath) file_path = Path(shpfilepath) directory_path = str(file_path.resolve().parent.name) rootname = Path(shpfilepath).stem txtpath=os.path.join(preFolderPath,rootname+'.txt') programpath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\LabelPortShipRasterSlice\Portshapefile2dota_AA.py" cmdtxt=r"{} {} -i {} -o {}".format(spacetySliceEnvPathExEPath,programpath,shpfilepath,txtpath) if os.system(cmdtxt) ==2: print("sucess:",cmdtxt) return "sucess: {}".format(cmdtxt) else: print("failed:",cmdtxt) return "failed: {}".format(cmdtxt) pass ######################################################## # 流程执行区 ######################################################## if __name__ == '__main__': srcFolderPath = r"D:\港口\港口" preFolderPath= r"D:\港口\港口dota" shpPaths = find_shp_files_pathlib(srcFolderPath) for shppath in shpPaths: preProcessLabelshapefile(shppath, preFolderPath)