Python directory operation Python traverses the folder and stores the result as XML

Source: Internet
Author: User
Linux servers have CentOS, fedora, etc., are pre-installed Python, version from 2.4 to 2.5, and the Windows type of server is also most installed Python, so as long as the local computer write a script, upload to the corresponding machine, at run time to modify parameters.

Python operations files and folders use the OS library, and the following code mainly uses several functions:

Os.listdir: List the files and folders under the directory
Os.path.join: Stitching to get the full path of a file/folder
Os.path.isfile: Determine if it is a file
Os.path.splitext: Remove a sub-section from a name

Here is the code for the directory operation

Copy the Code code as follows:


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 use the full path of the file, if you need to record the size of the file, time, type and other information, you can imitate code to expand.
Copy the Code code as follows:


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


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

Here is the code saved as XML

Copy the Code code as follows:


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 ()


The code is executed as follows

Copy the Code code as follows:


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, execute Python filesearch.py, and you can generate a file named Folder.xml.

If you want to run the program in Windows, you need to change the folder variable to a format under Windows, such as C:\\apache2\htdocs, and then execute C:\python25\python.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.