对python非常陌生,但在大学里有一些编码经验。我正在尝试找到一种方法,将每个mxd文件中的路径从绝对更改为相对,同时准备我的GIS项目数据库,以从本地托管转换为云托管(刚刚开始在家工作)。我发现了两个我认为可能有效但无法让它们协同工作的代码片段。来自ArcGIS的代码只在一个文件夹上工作,我希望它在根目录中的每个子目录上运行。谢谢你的帮助
ArcGIS Python部分
import arcpy, os
#workspace to search for MXDs
Workspace = r"c:\Temp\MXDs"
arcpy.env.workspace = Workspace
#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")
#set relative path setting for each MXD in list.
for file in mxdList:
#set map document to change
filePath = os.path.join(Workspace, file)
mxd = arcpy.mapping.MapDocument(filePath)
#set relative paths property
mxd.relativePaths = True
#save map doucment change
mxd.save()
子目录代码
... from fnmatch import fnmatch
...
... root = 'C:\\user\projects'
... pattern = "*.mxd"
...
... for path, subdirs, files in os.walk(root):
... for name in files:
... if fnmatch(name, pattern)
... mxdList = arcpy.ListFiles
...
此任务不需要使用
fnmatch
模块。arcpy.ListFiles('*.mxd')
函数中的通配符就足够了。不要通过files
循环,而是通过os.walk(root)
循环subdirs
请尝试以下操作:
相关问题 更多 >
编程相关推荐