python 判断是文件夹还是文件

2025-03-13 14:47:34
推荐回答(1个)
回答1:

def del_file_items(spath):
    import os

    paths = os.listdir(spath)

    for pa in paths:
        filepath = os.path.join(spath,pa)
        if os.path.isfile(filepath):
            try:
                os.remove(filepath)
            except os.error:
                print "remove %s error." %filePath
        elif os.path.isdir(filepath):
            try:
##在方法内可以引用自身
                del_file_items(filepath)
            except os.error:
                print "remove %s error." %filePath
    else:
        os.rmdir(spath)

这段代码给你做参考。