Python removes the file sample share
- 2020-04-02 13:25:10
- OfStack
Delete the file
os.remove( filename ) # filename: " The file name to delete "
Possible causes of the anomaly:
(1) the filename does not exist
(2) no operation permissions or read-only for filename files.
Delete all files and subfolders under the folder:
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)
Or use the rmtree function of the shutil module, which can also be cascaded