本节内容:
python os模块实例,删除指定文件夹中的所有内容。
例子:
#!/usr/bin/env python
#
#site: www.jb200.com
import os
def delete_file_folder(src):
'''delete files and folders'''
if os.path.isfile(src):
try:
os.remove(src)
except:
pass
elif os.path.isdir(src):
for item in os.listdir(src):
itemsrc=os.path.join(src,item)
delete_file_folder(itemsrc)
try:
os.rmdir(src)
except:
pass
if __name__=='__main__':
dirname=r'G:windows'
print delete_file_folder(dirname)