10
การลบโฟลเดอร์ในหลามแบบวนซ้ำ
ฉันมีปัญหาในการลบไดเรกทอรีว่าง นี่คือรหัสของฉัน: for dirpath, dirnames, filenames in os.walk(dir_to_search): //other codes try: os.rmdir(dirpath) except OSError as ex: print(ex) ข้อโต้แย้งdir_to_searchคือที่ที่ฉันผ่านไดเรกทอรีที่งานต้องทำ ไดเรกทอรีนั้นมีลักษณะดังนี้: test/20/... test/22/... test/25/... test/26/... โปรดทราบว่าโฟลเดอร์ด้านบนทั้งหมดว่างเปล่า เมื่อฉันเรียกใช้สคริปต์นี้โฟลเดอร์20, 25อยู่คนเดียวได้รับการลบ! แต่โฟลเดอร์25และ26ไม่ถูกลบแม้ว่าจะเป็นโฟลเดอร์เปล่า แก้ไข: ข้อยกเว้นที่ฉันได้รับคือ: [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test' [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012' [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10' [Errno 39] Directory …