Today for the first time the file traversal, the time of their own recursive writing also debugging long, (mainly because of the problem of the separation of symbols), and later found the Os.walk method, can not help to share with you.
First look at the code:
Import OS
For I in Os.walk (' C: ' +os.sep+ ' ant '):
Print I[1]
Here is the output:
C:\ant
C:\ant\bin
C:\ant\docs
C:\ant\docs\ant2
C:\ant\docs\antlibs
C:\ant\docs\antlibs\antunit
C:\ant\docs\antlibs\compress
C:\ant\docs\antlibs\dotnet
C:\ant\docs\antlibs\props
C:\ant\docs\antlibs\svn
C:\ant\docs\images
C:\ant\docs\manual
C:\ant\docs\manual\api
c:\ant\docs\manual\api\org
C:\ant\docs\manual\api\org\apache
C:\ant\docs\manual\api\org\apache\tools
C:\ant\docs\manual\api\org\apache\tools\ant
C:\ant\docs\manual\api\org\apache\tools\ant\dispatch
C:\ant\docs\manual\api\org\apache\tools\ant\filters
There's a long back.
If you do not use this method, the traversal will also achieve the same effect. But it's a lot easier to use Os.walk. This method returns a ternary tupple (Dirpath, Dirnames, filenames),
The first one is the starting path,
The second is a folder under the starting path,
The third one is the file under the starting path.
Dirpath is a string that represents the path to the directory,
Dirnames is a list that contains the names of all subdirectories under Dirpath,
Filenames is a list that contains the name of a non-directory file. These names do not contain path information, and you need to use Os.path.join (Dirpath, name) if you need to get the full path.
Here is what you can see returned by the Os.walk method.
Code:
Import OS
For I in Os.walk (' C: ' +os.sep+ ' ant '):
Print I
Output:
(' C:\\ant ', [' bin ', ' docs ', ' etc ', ' lib ', ' Project '], [' Fetch.xml ', ' get-m2.xml ', ' INSTALL ', ' KEYS ', ' LICENSE ', ' NOTICE ', ' README ', ' whatsnew '])
(' C:\\ant\\bin ', [], [' Ant ', ' ant.bat ', ' ant.cmd ', ' antenv.cmd ', ' antrun ', ' antrun.bat ', ' antrun.pl ', ' complete-ant-cmd.pl ', ' envset.cmd ', ' lcp.bat ', ' runant.pl ', ' runant.py ', ' runrc.cmd '])
(' C:\\ant\\docs ', [' Ant2 ', ' antlibs ', ' images ', ' manual ', ' projects ', ' slides ', ' webtest '], [' antnews.html ', ' ant_in_ ' Anger.html ', ' ant_task_guidelines.html ', ' appendix_e.pdf ', ' breadcrumbs.js ', ' bugs.html ', ' bylaws.html ', ' Contributors.html ', ' external.html ', ' faq.html ', ' favicon.ico ', ' index.html ', ' legal.html ', ' LICENSE ', ' license.html ' , ' mail.html ', ' mission.html ', ' nightlies.html ', ' page.css ', ' problems.html ', ' projects.html ', ' resources.html ', ' Svn.html '])
(' C:\\ant\\docs\\ant2 ', [], [' actionlist.html ', ' features.html ', ' functionalrequirements.html ', ' Original-specification.html ', ' requested-features.html ', ' requested-features.txt ', ' VFS.txt '])
(' C:\\ant\\docs\\antlibs ', [' antunit ', ' compress ', ' dotnet ', ' props ', ' svn '], [' bindownload.cgi ', ' bindownload.html ', ' charter.html ', ' index.html ', ' proper.html ', ' sandbox.html ', ' srcdownload.cgi ', ' srcdownload.html ']
(' C:\\ant\\docs\\antlibs\\antunit ', [], [' index.html '])
(' c:\\ant\\docs\\antlibs\\compress ', [], [' index.html '])
(' C:\\ant\\docs\\antlibs\\dotnet ', [], [' index.html '])
(' C:\\ant\\docs\\antlibs\\props ', [], [' index.html '])
...
Of course, there's a long back.
With this function, it is convenient to traverse a folder or traverse a file.
Here is my own recursive implementation of the traversal file method.
Code:
def listdir (Leval,path):
For I in Os.listdir (path):
Print (' | ' * (Leval + 1) + i)
If Os.path.isdir (path+i):
Listdir (leval+1, Path+i)
Path = ' C: ' +os.sep+ ' ant '
#或者直接 path= ' c:/ant '
Print (PATH+OS.SEP)
Listdir (0, PATH+OS.SEP)
Here is the output:
C:\ant\
| Bin
| | Ant
| | Ant.bat
| | Ant.cmd
| | Antenv.cmd
| | Antrun
| | Antrun.bat
| | antrun.pl
| | complete-ant-cmd.pl
| | Envset.cmd
| | Lcp.bat
| | runant.pl
| | runant.py
| | Runrc.cmd
| Docs
| | Ant2
| | Antlibs
| | Antnews.html
| | Ant_in_anger.html
| | Ant_task_guidelines.html
| | Appendix_e.pdf
| | Breadcrumbs.js
| | Bugs.html
| | Bylaws.html
| | Contributors.html
| | External.html
| | Faq.html
| | Favicon.ico
| | Images
| | Index.html
| | Legal.html
| | LICENSE
| | License.html
| | Mail.html
| | Manual
| | Mission.html
| | Nightlies.html
| | Page.css
| | Problems.html
| | Projects
| | Projects.html
| | Resources.html
| | Slides
| | Svn.html
| | WebTest
| etc
| | Ant-bootstrap.jar
| | Changelog.xsl
| | Checkstyle
| | Coverage-frames.xsl
| | Jdepend-frames.xsl
| | Jdepend.xsl
| | Junit-frames-xalan1.xsl
| | Junit-frames.xsl
| | Junit-noframes.xsl
| | Log.xsl
| | Maudit-frames.xsl
| | Mmetrics-frames.xsl
| | Tagdiff.xsl
| Fetch.xml
| Get-m2.xml
| INSTALL
| KEYS
| Lib
| | Ant-1.8.0.pom
| | Ant-1.8.0.pom.md5
| | Ant-1.8.0.pom.sha1
| | ant-1.8.0.pom.sha512
..
If you just want to get the folder and not the files, put the things you want to do
If Os.path.isdir (path+i):
The inside is good, for example: print ()
O (∩_∩) o~
Python Traversal directory with Os.walk