Skip to content

Instantly share code, notes, and snippets.

@zhaojunhhu
Last active May 10, 2017 08:19
Show Gist options
  • Save zhaojunhhu/2469f086e778c8f581f52130fb661817 to your computer and use it in GitHub Desktop.
Save zhaojunhhu/2469f086e778c8f581f52130fb661817 to your computer and use it in GitHub Desktop.
删除文件夹下所有文件和子文件夹
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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
dirname=r'G:\windows'
print delete_file_folder(dirname)
os.mkdir(dirname)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import shutil os
shutil.rmtree('要清空的文件夹名')
os.mkdir('要清空的文件夹名')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment