Python Learning Notes OS module common items

Source: Internet
Author: User

Delete empty directories in the specified directory tree #! /usr/bin env pythonimport osimport sysdir =sys.argv[1]if os.path.isdir (dir): For Root,dirs,files in Os.walk (dir,topdown= False): #从最里向外遍历 for D in Dirs:if not Os.listdir (Os.path.join (root,d)): #判断目录是否为空 Os.rmdir (Os.path. Join (root,d)) Else:print '%s is not dir '%dir

The use of the Os.walk () function

For Root,dirs,files in Os.walk (Path,topdown=true,onerro=none)

Returns a ternary tuple, root for each traversed path, DIRS returns a list of directories, and files returns a file list

Topdown default (True) from outside, False to from inside Out

OS Module Common items

Os.listdir (PATH)

OS.GETCWD ()

Os.chdir (PATH)

Os.rmdir (PATH)

Os.remove ()

Os.path.split (PATH)

Os.path.join (Path,filename)

Os.path.isdir ()

Os.path.isfile ()

Os.path.exists ()

Os.path.getsize ()

Os.path.basename ()

Os.path.dirname ()

Os.sep


Python Learning Notes OS module common items

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.