Python Module Application case Highlights

Source: Internet
Author: User

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/props
View 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

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.