1. OS Module
1.1 Python os.walk Usage and examples
Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false)
Can get a ternary tupple (Dirpath, Dirnames, filenames),
The first is the starting path, the second is the folder under the starting path, and the third 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.
Recursive directory with A For loop
[[Email protected] ~]#Cat os_walk.pyImportOsfiledir='./sadoc'defCCC (): forRoot,dirs,filesinchOs.walk (filedir): forDirinchdirs:Print(Os.path.join (root,dir)) forFileinchFiles:Print(Os.path.join (root,file))#Os.system ("pause")if __name__=='__main__': CCC () [[email protected]~]#python os_walk.py./sadoc/. svn./sadoc/tags./sadoc/Branch./sadoc/trunk./sadoc/.svn/text-base./sadoc/.svn/tmp./sadoc/.svn/prop-base./sadoc/.svn/props./sadoc/.svn/entries./sadoc/.svn/tmp/text-base./sadoc/.svn/tmp/prop-base./sadoc/.svn/tmp/props./sadoc/tags/. svn./sadoc/tags/.svn/text-base./sadoc/tags/.svn/tmp./sadoc/tags/.svn/prop-base./sadoc/tags/.svn/props./sadoc/tags/.svn/entries./sadoc/tags/.svn/tmp/text-base./sadoc/tags/.svn/tmp/prop-base./sadoc/tags/.svn/tmp/props./sadoc/branch/. svn./sadoc/branch/.svn/text-base./sadoc/branch/.svn/tmp./sadoc/branch/.svn/prop-base./sadoc/branch/.svn/props./sadoc/branch/.svn/entries./sadoc/branch/.svn/tmp/text-base./sadoc/branch/.svn/tmp/prop-base./sadoc/branch/.svn/tmp/props./sadoc/trunk/. svn./sadoc/trunk/.svn/text-base./sadoc/trunk/.svn/tmp./sadoc/trunk/.svn/prop-base./sadoc/trunk/.svn/props./sadoc/trunk/.svn/entries./sadoc/trunk/.svn/tmp/text-base./sadoc/trunk/.svn/tmp/prop-base./sadoc/trunk/.svn/tmp/propsView Code
Application Scenarios:
1) Traverse directory to replace all files in the file contents
2) file batch rename under directory
Python Module Application case Highlights