Python traverses code instances for sub-files and all subfolders

Source: Internet
Author: User
Recently see Ecshop to find information on the Internet, found a lot of explanation ecshop file structure is not comprehensive, so want to get a out. But it's a tedious and time-consuming job, and I've written a Python script that recursively iterates through all the files and all subdirectories in the directory and logs the results into an. xml file (because you want to use notepad++ 's code folding feature, so you use an. xml file).

Here's the Python code:

#-*-coding:cp936-*-############################################# # Written by Qian_f # # Get a list of file paths and write Into the current directory build test.txt # ############################################# import os def getfilelist (filepath, tabnum=1): Simple Path = Os.path.split (filepath) [1] returnstr = simplepath+ "directory <>" + "\ n" returndirstr = "" Returnfilestr = "" fi Lelist = Os.listdir (filepath) for NUM in range (len (filelist)): Filename=filelist[num] If Os.path.isdir (filepath+ "/" +filename): Returndirstr + = "\ T" *tabnum+getfilelist (filepath+ "/" +filename, tabnum+1) else:returnfilest         r + = "\ t" *tabnum+filename+ "\ n" returnstr + = returnfilestr+returndirstr return returnstr+ "\ T" *tabnum+ "</>\n" Path = raw_input ("Please enter file path:") Usefulpath = path.replace (' \ \ ', '/') if Usefulpath.endswith ("/"): Usefulpath = Usefulpa   TH[:-1] If not os.path.exists (Usefulpath): print "Path Error!" Elif not Os.path.isdir (usefulpath): print "input is not directory!" Else: FileList = Os.listdir (USEfulpath) O=open ("Test.xml", "w+") O.writelines (Getfilelist (Usefulpath)) O.close () print "Success!" Please see the Test.xml file "

After executing the Python script, the Test.xml file is generated in the current directory, and the file structure of the specified directory is visible using notepad++ open (ANSI encoding). Here is the file Structure section of the upload directory under the ecshop I generated:

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.