Skip to content

Instantly share code, notes, and snippets.

@Gracker
Last active August 29, 2015 14:02
Show Gist options
  • Save Gracker/d46a03dad31916e069ab to your computer and use it in GitHub Desktop.
Save Gracker/d46a03dad31916e069ab to your computer and use it in GitHub Desktop.
Folder to xml
'''
folder2xml.py
create by Jianwu Gao ,
2014.6.10
'''
import os
import sys
from xml.dom.minidom import Document
def search(folder, filter, allfile, folderpath):
folders = os.listdir(folder)
for name in folders:
curname = os.path.join(folder, name)
isfile = os.path.isfile(curname)
if isfile:
ext = os.path.splitext(curname)[1]
count = filter.count(ext)
if count>0:
cur = myfile()
cur.name = name
cur.absolutePath = curname.replace(folderpath,"")
allfile.append(cur)
else:
search(curname, filter, allfile,folderpath)
return allfile
class myfile:
def __init__(self):
self.name = ""
self.absolutePath = ""
def generate(allfile, xml):
doc = Document()
root = doc.createElement("root")
doc.appendChild(root)
for myfile in allfile:
file = doc.createElement("file")
file.setAttribute("name", myfile.name)
file.setAttribute("path", myfile.absolutePath)
root.appendChild(file)
print doc.toprettyxml(indent="")
f = open(xml, 'w+')
f.write(doc.toprettyxml(indent=""))
f.close()
if __name__ == '__main__':
folder = sys.argv[1]
foldername = sys.argv[2]
filter = [".png",".jpg"]
allfile = []
allfile = search(folder, filter, allfile,folder)
len = len(allfile)
print "found: " + str(len) + " files"
xml = folder + "/" + foldername + ".xml"
generate(allfile, xml)
@Gracker
Copy link
Author

Gracker commented Jun 12, 2014

1.脚本使用:
python -u "/Users/gaojack/Documents/test/folder2xml.py" /Users/gaojack/Documents/test/resourcepackage folder
1.1 参数说明:
-u ——— 不启用缓冲
"/Users/gaojack/Documents/test/folder2xml.py” ——— folder2xml.py脚本的位置
/Users/gaojack/Documents/test/resourcepackage ——— 资源根目录的位置
folder ——— 生成的文件名

2.生成的文件为 xml 文件 内容如下:

3.生成的文件位置在资源根目录中,
如资源根目录为/Users/gaojack/Documents/test/resourcepackage ,文件名为 path,那么生成的文件是/Users/gaojack/Documents/test/resourcepackage/path.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment