import os
dhvideopath = r'C:\RecordDownload'
aa = os.path.exists(r'C:\RecordDownload\NVR_ch1_main_20171231230001_20180101000001.asf')
bb = os.listdir(r'C:\RecordDownload')
#print (aa,bb)
if len(bb) == 0:
print ('empty')
else:
print('There is something,Delete it')
for file in bb:
try:
os.remove(dhvideopath+'\\'+file)
except IOError:
print ('系统错误,无法删除文件-'+file+',可能被占用')
continue
else:
print (file+'has been deleted')
if len(os.listdir(dhvideopath)) == 0:
print ('Delete complete')
else:
dhvideopath = r'C:\RecordDownload'
aa = os.path.exists(r'C:\RecordDownload\NVR_ch1_main_20171231230001_20180101000001.asf')
bb = os.listdir(r'C:\RecordDownload')
#print (aa,bb)
if len(bb) == 0:
print ('empty')
else:
print('There is something,Delete it')
for file in bb:
try:
os.remove(dhvideopath+'\\'+file)
except IOError:
print ('系统错误,无法删除文件-'+file+',可能被占用')
continue
else:
print (file+'has been deleted')
if len(os.listdir(dhvideopath)) == 0:
print ('Delete complete')
else:
print ('someting cannot be deleted')
输出: