python文件目錄遍歷保存成xml文件代碼

Linux服務器有CentOS、Fedora等,都預先安裝了Python,版本從2.4到2.5不等,而Windows類型的服務器也多數安裝了Python,所以只要在本機寫好一個腳本,上傳到對應機器,在運行時修改參數便可。php

Python操做文件和文件夾使用的是os庫,下面的代碼中主要用到了幾個函數:html

os.listdir:列出目錄下的文件和文件夾python

os.path.join:拼接獲得一個文件/文件夾的全路徑apache

os.path.isfile:判斷是不是文件bootstrap

os.path.splitext:從名稱中取出一個子部分數組

 

下面是目錄操做的代碼服務器

 代碼以下app

複製代碼dom

def search(folder, filter, allfile):
    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 = curname
                allfile.append(cur)
        else:
            search(curname, filter, allfile)
    return allfile函數

在返回文件的各類信息時,使用自定義類allfile來保存文件的信息,在程序中只用到了文件的全路徑,若是須要同時記錄文件的大小、時間、類型等信息,能夠仿照代碼進行擴充。

 代碼以下

複製代碼

class myfile:
    def __init__(self):
        self.name = ""

獲得存儲文件信息的數組後,還能夠將其另存成xml格式,下面是代碼,在使用時,須要從Document中導入xml.dom.minidom

下面是保存爲xml的代碼

 代碼以下

複製代碼

def generate(allfile, xml):
    doc = Document()

    root = doc.createElement("root")
    doc.appendChild(root)

    for myfile in allfile:
        file = doc.createElement("file")
        root.appendChild(file)

        name = doc.createElement("name")
        file.appendChild(name)
        namevalue = doc.createTextNode(myfile.name)
        name.appendChild(namevalue)

    print doc.toprettyxml(indent="")
    f = open(xml, 'a+')
    f.write(doc.toprettyxml(indent=""))
    f.close()

執行的代碼以下

 代碼以下

複製代碼

if __name__ == '__main__':
    folder = "/usr/local/apache/htdocs"
    filter = [".html",".htm",".php"]
    allfile = []
    allfile = search(folder, filter, allfile)
    len = len(allfile)
    print "found: " + str(len) + " files"

    xml = "folder.xml"
    generate(allfile, xml)

Linux命令行狀態下,執行Python filesearch.py,即可以生成名爲folder.xml的文件。

若是要在Windows中運行該程序,須要把folder變量改爲Windows下的格式,例如c:\apache2htdocs,而後執行c:python25python.exe filesearch.py(這裏假設python的安裝目錄是c:python25

 

源碼整理:

import osimport sysfrom xml.dom.minidom import Documentclass myfile:    def __init__(self):        self.name = ""def search(folder, filter, allfile):    print ("in search.....")    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 = curname                allfile.append(cur)        else:            search(curname, filter, allfile)    return allfiledef generate(allfile, xml):    doc = Document()    print ("in generate.........")    root = doc.createElement("root")    doc.appendChild(root)    for myfile in allfile:        file = doc.createElement("file")        root.appendChild(file)        name = doc.createElement("name")        file.appendChild(name)        namevalue = doc.createTextNode(myfile.name)        name.appendChild(namevalue)    print doc.toprettyxml(indent="")    f = open(xml, 'a+')    f.write(doc.toprettyxml(indent=""))    f.close()if __name__ == '__main__':     folder = "D:\\mine\\bootstrap-3.3.2\\docs"     filter = [".html",".htm",".php"]     allfile = []      allfile = search(folder, filter, allfile)     len = len(allfile)     print "found: " + str(len) + " files"     xml = "folder.xml"     generate(allfile, xml)        

相關文章
相關標籤/搜索