如何删除我的驱动器上的空目录

时间:2017-08-23 15:14:20

标签: python file directory filesystems

如何删除D:\驱动器上的空目录让我们说.... 我用Python编写,出于某种原因,我的代码不起作用。 我无法弄清楚问题

感谢您的帮助...... 代码:

import os
path = 'D:\\'
dire = []

for file in os.listdir(path):
    print(file)
    dire.append(file)

try:
    for file in dire:
        new_path = 'D:\{0}'.format(file+"\\")
        if os.listdir(new_path) == []:
            os.rmdir(file)
            print(file+" has been deleted")
        else:
            print("directory "+file+" is not empty")

except (FileNotFoundError, NotADirectoryError):
    print(file+" could not be located, or opened...")
    dire.remove(file)

0 个答案:

没有答案