Python file directory traversal saved as XML file code

Source: Internet
Author: User
Tags file size

Python operates files and folders using an OS library, and the following code uses several functions:

Os.listdir: Lists files and folders under the directory

Os.path.join: Stitching to get a full path to a file/folder

Os.path.isfile: Judge whether it is a file

Os.path.splitext: Take a child part out of a name

The following is the code for the directory operation

The code is as follows Copy Code
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

In the return of various information of the file, use the custom class Allfile to save the file information, in the program only to use the full path of the file, if you need to record the file size, time, type and other information, can be modeled after the code to expand.

The code is as follows Copy Code

Class MyFile:
def __init__ (self):
Self.name = ""

Once you have an array of stored file information, you can also save it as an XML format, which is code, which you need to import from document when you use the Xml.dom.minidom

Here is the code that is saved as XML

The code is as follows Copy Code

def generate ( Allfile, XML):
    doc = Document ()

    root = doc.createelement ("root")
& nbsp;   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 ()

The code executed is as follows

The code is as follows Copy Code

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)

In the Linux command line state, a file named Folder.xml can be generated by executing the Python filesearch.py.

If you want to run the program in Windows, you need to change the folder variable to a format under Windows, such as C:\apache2htdocs, and then execute the C:python25python.exe Filesearch.py (This assumes that the Python installation directory is c:python25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.