python os.remove删除文件(检查是否被占用,处理被占用无法删除时的系统报错)

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:

print ('someting cannot be deleted')


输出:

python os.remove删除文件(检查是否被占用,处理被占用无法删除时的系统报错)_第1张图片

你可能感兴趣的:(Python学习笔记)