Python directory Operations Python traverse the folder and store the result as Xml_python

Source: Internet
Author: User

Linux server has CentOS, fedora and so on, all pre-installed Python, version ranging from 2.4 to 2.5, and Windows type of server is mostly installed Python, so as long as the local write a script, upload to the corresponding machine, at run time modify parameters.

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

Copy 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 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.

Copy 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 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

Copy 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 executed is as follows

Copy 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, 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:\\apache2\htdocs, and then execute the C:\python25\python.exe Filesearch.py (This assumes that the Python installation directory is c:\python25)

Related Article

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.