python 如何判断文件夹为空文件夹?

2025-03-03 10:39:54
推荐回答(1个)
回答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:

2、os.remove(filepath)

except os.error:

print "remove %s error." %filePath

elif os.path.isdir(filepath):

try:

3、##在方法内可以引用自身

del_file_items(filepath)

except os.error:

print "remove %s