我是python初学者,开始编写下面的脚本。它已经起作用了,但方式不对。现在我被卡住了,我需要一些帮助。我使用手术室步行以便在Windows中以文件路径的XML文件形式获取索引。我还添加了脚本的当前结果,以及我需要输出的内容。在
结果和我需要的不同之处在于,子文件夹在文件之前。在
##This Script creates an Index as XML file of a filepath in Windows
import os
#variable
CrawlingStartpoint = r"D:\DATA\WorldDem"
XMLfile = r"xml_index.xml"
XMLLocation = r"D:\DATA\\"
XMLFileLocation = XMLLocation + XMLfile
text = ""
#Standard Starting text for the XML file
text += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ShoeBox>\n<Version>2011</Version>\n<FileGroup>\n<Title>ShoeBox: "
text += XMLfile + "</Title>\n<Description>" + CrawlingStartpoint + "</Description>\n<Expanded>false</Expanded>\n<AddOutputs>false</AddOutputs>\n"
#Scan all folders, subfolders and files. It writes foldernames, subfolder names and files.
#It write a closegroup for every subfolder to build the filestructure in a XML file.
startdept = CrawlingStartpoint.split('\\')
startdept = len(startdept) - 1
old = startdept - 1
for root, dirs, files in os.walk(CrawlingStartpoint, topdown=True):
path = root.split('\\')
if (len(path) - 1) < old:
text += (((old - (len(path) - 1))+1)*'</FileGroup>\n')
old = (len(path) -1)
elif (len(path) - 1) == old:
text += "</FileGroup>\n"
if os.path.join(root) <> CrawlingStartpoint:
text += "<FileGroup>\n<Title>" + os.path.basename(root) + "</Title>\n<Description>noop</Description>\n<Expanded>false</Expanded>\n<AddOutputs>false</AddOutputs>\n"
for filename in files:
if filename.endswith(".img") or filename.endswith(".jp2") or filename.endswith(".tif"):
text += "<File>\n<Path>" + os.path.join(root, filename) + "</Path>\n<Type>raster</Type>\n<Description></Description>\n</File>\n"
elif filename.endswith(".shp") or filename.endswith(".dxf"):
text += "<File>\n<Path>" + os.path.join(root, filename) + "</Path>\n<Type>vector</Type>\n<Description></Description>\n</File>\n"
if (len(path) - 1) > old:
old = old + 1
text += (((len(path) -1) - startdept)*'</FileGroup>\n')
#Standard Closing text for the XML file
text += "</FileGroup>" + "\n" + "</ShoeBox>"
#Write the filelocations stored in text in the textfile
myfile = open(XMLFileLocation,'a')
myfile.write(text)
myfile.close()
<?xml version="1.0" encoding="UTF-8"?>
<ShoeBox>
<Version>2011</Version>
<FileGroup>
<Title>ShoeBox: xml_index.xml</Title>
<Description>D:\DATA\WorldDem</Description>
<Expanded>false</Expanded>
<AddOutputs>false</AddOutputs>
<FileGroup>
<Title>srtm_geotiff_download</Title>
<Description>noop</Description>
<Expanded>false</Expanded>
<AddOutputs>false</AddOutputs>
<File>
<Path>D:\DATA\WorldDem\srtm_geotiff_download\srtm1_90m_v41.img</Path>
<Type>raster</Type>
<Description></Description>
</File>
</FileGroup>
<FileGroup>
<Title>viewfinderpanoramas</Title>
<Description>noop</Description>
<Expanded>false</Expanded>
<AddOutputs>false</AddOutputs>
<FileGroup>
<Title>Compressed</Title>
<Description>noop</Description>
<Expanded>false</Expanded>
<AddOutputs>false</AddOutputs>
</FileGroup>
<FileGroup>
<Title>Modellen</Title>
<Description>noop</Description>
<Expanded>false</Expanded>
<AddOutputs>false</AddOutputs>
</FileGroup>
<File>
<Path>D:\DATA\WorldDem\viewfinderpanoramas\mosaic_vfp_90m_bewerkt_20140211.img</Path>
<Type>raster</Type>
<Description></Description>
</File>
</FileGroup>
</FileGroup>
</ShoeBox>
我不认为有什么简单的方法可以做你想做的。至少,如果您是手工编写XML的话,就不会有。在
原因是您需要从根目录开始构建XML文档,从最外面的目录开始,生成最顶端的
<FileGroup>
标记。另一方面,您希望在写出每个文件夹中的文件之前深入到子文件夹中,这样叶目录中的文件将首先出现,根目录中的文件将最后出现。这两个目标相互矛盾。在我认为有两种策略可以用来解决这个问题。在
第一种方法是使用更复杂的方法来编写XML文件。例如,如果使用允许动态创建文档元素的库,而不是仅使用字符串操作,则可以在文件信息上方插入子文件夹信息,即使按照现在相同的顺序处理它们(先处理文件)。我对Python的XML库不是很了解,所以我在这里没有任何具体的建议,但可能开始的地方是关于标准库中^{} 包的文档(您可能只想要一个像
xml.etree.Etree
这样的子模块,但我不确定这是否最适合您的任务,所以请查看该包并阅读其中任何一个零件看起来合适)。在另一个选择是改变获取数据的方式。{cd5>如果你想让某个更高级别的文件返回,那么你应该先把它放回更高的级别。关于这一点的棘手之处在于,您需要先构建更高级别的
<FileGroup>
标记,然后才能开始处理它们所在的文件夹。你需要根据文件夹的路径推断出你所处的深度,但是你已经做了一些这方面的工作,所以可能不会太复杂。(顺便说一句,您确实应该使用os.path
模块中的函数来进行文件路径操作,而不是像my_path.split("\\")
那样做。)在Blcknght的评论之后,我删除了“\\”和操作系统特有的东西。这个脚本现在可以在windows和linux上运行了,而且效果很好。我在主脚本上重新开始了。我没有使用xml包,但是为此我创建了两个函数,它们使用操作系统列表目录()而不是手术室步行. 下面是脚本。在
如果你读了你注意到的主要部分的脚本有很多“for”循环和“If”语句。对于每个子文件夹,脚本将重复执行相同的操作。但我想不出,怎么把这个“for”循环。在
我可以找出在给定的搜索文件夹中有多少子文件夹。但是我需要一些帮助如何为主脚本创建一个循环。我需要一些帮助:)
相关问题 更多 >
编程相关推荐