I am trying to write an up to date Python function for removing files securely. This quest led me to this Stack Overflow question, the answers to which, as I understand it, give me two options:
- Install the srmcommand, and call that usingsubprocessor something similar.
- Use a given custom function.
I have to reject option (1) because, although I am using Linux myself, I am writing code destined for a custom PIP package, which needs to be as lightweight and portable as possible.
As for option (2): I have distilled the various functions supplied in the answers to the aforementioned question into one function:
def secure_delete(path_to_file, passes=1):
    length = os.path.getsize(path_to_file)
    with open(path, "br+", buffering=-1) as file_to_overwrite:
        for _ in range(passes):
            file_to_overwrite.seek(0)
            file_to_overwrite.write(os.urandom(length))
    os.remove(path_to_file)
Now this looks like we could be getting somewhere, but I still have some queries:
- I believe that the os.pathstuff has largely been superceded bypathlib. Is that correct?
- But what about that os.urandom(length)call? Is that the most efficient, up to date way of doing that?
- I understand what that passesvariable is doing, but I do not understand what the point of it is. Is there really all the much to be gain, from a security point of view, by overwriting multiple times?
 
    