Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I securely wipe a file / directory in Python?

Is there any module which provides somehow basic "secure" deletion, sth. like the Linux utility "wipe", e.g.

import securitystuff

securitystuff.wipe( filename )

I need to protect company source code which should not be easily retrievable anymore.

P.S. Yes I know "wipe" is not perfect, e.g. on journalling filesystem. But the security demand is not tooo high.

like image 665
Marcus Stein Avatar asked Jun 21 '10 09:06

Marcus Stein


People also ask

How do you permanently delete a file in Python?

Using the os module in python To use the os module to delete a file, we import it, then use the remove() function provided by the module to delete the file. It takes the file path as a parameter.

Which method is used to delete a directory in Python?

remove() method in Python is used to remove or delete a file path.


1 Answers

There is no such function in standard library and a naive implementation which overwrites each byte of file with a random byte is not too difficult to do e.g.

 f = open(path, "wb")
 f.write("*"*os.path.getsize(path))
 f.close()
 os.unlink(path)

But as suggested in thread http://mail.python.org/pipermail/python-list/2004-September/899488.html this doesn't guarantee wiping due to many reasons, e.g. disk cache, remapping of disk sectors etc etc

So instead of implementing your own wipe easiest would be to call linux wipe from python.

Alternate option is to use srm

like image 120
Anurag Uniyal Avatar answered Oct 04 '22 07:10

Anurag Uniyal