0

Is there a way to securely overwrite a file fed into a Python Script?

How would I go about overwriting a file (not the text inside the file) with random characters? I know you can do this pretty easily in other languages but I haven't found anything super helpful in regards to Python.

By "secure" I'm referring to overwriting the file contents.

I'd prefer not to use a library but either way works.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    "Securely" and "prefer not to use a library" are conflicting goals, and the "securely" goal is much more important. Don't try to write this yourself. – user2357112 Oct 22 '20 at 19:45
  • This is going to depend on the drive technology used. – user2357112 Oct 22 '20 at 19:48
  • The reason I say that is because you can do this pretty easily in Ruby without the use of any external libraries, my goal is to make this program lightweight and portable, and sure it could be done with a library but I'd like to have the experience of writing this one myself. Thank you for your comment! – Clemens Dotson Oct 22 '20 at 19:49
  • I couldn't find any built-in functionality for this in Ruby. I suspect you misinterpreted something. – user2357112 Oct 22 '20 at 19:51
  • @user2357112supportsMonica As for the drive technology, if I have the ability to overwrite the file's data from Python (not something super low level), would it matter what kind of drive technology is used? – Clemens Dotson Oct 22 '20 at 19:51
  • 1
    On an SSD, "overwriting" the file's data will probably write new data somewhere else and leave the old physical storage untouched. – user2357112 Oct 22 '20 at 19:52
  • https://stackoverflow.com/questions/8508885/how-can-i-securely-erase-a-file I'm just learning about this, so it's definitely possible I misinterpreted this. – Clemens Dotson Oct 22 '20 at 19:54
  • 4
    From the Ruby question you cite: https://stackoverflow.com/a/8511527 opens files, queries their sizes, and writes raw bytes to them. All these operations are elementary in Python too, so there's no barrier to porting that solution to Python. Whether this approach performs as desired is another question—as @user2357112supportsMonica points out, on an SSD it may do more harm than good, and there may be no good solution except ensuring your drive is encrypted in the first place. It's not clear to me what you mean by "overwriting a file (not the text inside the file) "—what's the difference? – jez Oct 22 '20 at 20:45

0 Answers0