Asked By
benjamin
460 points
N/A
Posted on - 11/16/2012
Hello fellows,
Is there a python file eraser? My friend is using python programming language for building an important program or system; she asks me if I have any idea about the python file eraser. I want to help my friend to research about this. Share with me your answers and ideas.
Regards,
Benjamin.
Is there a python file eraser?
Hello Benjamin,
Your question is somewhat unclear. But assuming that you were trying to find some method to delete files using Python, here are a couple of methods you can follow.
You can do use the remove () function from the module 'OS'
import os
os.remove('Path/To/File.ext')
Or try this code below.
#!/usr/bin/env python
import os
def nukedir(dir):
if dir[-1] == os.sep: dir = dir[:-1]
files = os.listdir(dir)
for file in files:
if file == '.' or file == '..': continue
path = dir + os.sep + file
if os.path.isdir(path):
nukedir(path)
else:
os.unlink(path)
os.rmdir(dir)
nukedir("/home/mb/test");