python 如何判断文件夹为空文件夹??求可执行代码

2025-03-04 04:25:55
推荐回答(3个)
回答1:

work_path = '你的目录'

if not os.listdir(work_path):
    print '目录为空'

如果解决了您的问题请采纳!
如果未解决请继续追问

回答2:

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

回答3:

#coding=utf-8
import os
import os.path

#定义文件夹路径
folderpath='d:\\pvs'

#设置文件夹内容是否为空标识符
empty=False

#os.path.exists(folderpath.encode('GBK'))路径如果存在,返回True
if(os.path.exists(folderpath.encode('GBK'))==False):
#文件夹不存在时
#doing something...
else:
for root,dirs, files in os.walk(folderpath.encode('GBK')):
if(len(files)==0):
empty=True
if(empty):
#文件夹内容为空
#doing something...