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)