os.rmdir(path)
path -- 這是一個目錄,需要刪除的路徑。
# !/usr/bin/python3 import os, sys os.chdir("d:\\tmp") # listing directories print ("the dir is: %s" %os.listdir(os.getcwd())) # removing path os.rmdir("newdir") # listing directories after removing directory path print ("the dir is:" %os.listdir(os.getcwd()))
the dir is: ['Applicationdocs.docx', 'book.zip', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'newdir', 'python2'] Traceback (most recent call last): File "test.py", line 8, in os.rmdir("newdir") OSError: [WinError 145] The directory is not empty: 'newdir'
the dir is: ['Applicationdocs.docx', 'book.zip', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'newdir', 'python2'] the dir is: ['Applicationdocs.docx', 'book.zip', 'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 'java.ppt', 'python2']