os.removedirs(path)
path -- 這是一個目錄,是需要刪除的路徑。
# !/usr/bin/python3 import os, sys os.chdir("d:\\tmp") # listing directories print ("The dir is: %s" %os.listdir(os.getcwd())) # removing os.removedirs("home\\monthly\\daily") # listing directories after removing directory print ("The dir after removal is:" %os.listdir(os.getcwd()))
The dir is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'ParallelPortViewer'] The dir after removal is: ['Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'ParallelPortViewer']