#!/usr/bin/env python
'递归法删除当前目录下的空文件'
import os
def deleteEmptyFile(path):
try:
os.chdir(path)
except WindowsError, e:
print e
return
else:
cwd = os.getcwd()
listdir = os.listdir(cwd)
for dirfile in listdir:
os.chdir(cwd) #重新设置当前目录
if os.path.isfile(dirfile):
if not os.path.getsize(dirfile):
os.remove(dirfile)
print 'delete file:', dirfile
if os.path.isdir(dirfile):
if len(os.listdir(cwd + '\\' + dirfile)) == 0:
os.removedirs(dirfile)
print 'delete file:', dirfile
else:
deleteEmptyFile(path + '\\' + dirfile)
deleteEmptyFile('C:\Documents and Settings\Administrator\桌面\\testfile')
11-02
11-02
11-02
11-02