DEV Community

Cover image for HOW TO SHRED A FILE IN PYTHON
Geof
Geof

Posted on

HOW TO SHRED A FILE IN PYTHON

Except you are into tech or you are one of those curious minds, you’d probably think that hitting the delete button permanently deletes a file in your hard-drive or any other storage device.

Yes! you heard right and am not talking about restoring files from the recycle bin, am talking about the files that has been deleted even from the recycle bin.

As scary as it sounds, those pretty sensitive private data in that USB you gave out on last boxing-day, which you took your precious time to delete can actually be restored using tools like testdisk.

Now the question is how do you make sure that this data are permanently deleted beyond recovery? Ever heard of shredding? Yes there is a physical machine for that in the real world, but this time am talking about the digital shredding of those unwanted digital files within your computer.

If you use linux distro like ubuntu, there’s a package called shred and it’s shipped with ubuntu by default. You can just use it via the command-line interface(CLI).

There’re other packages for shredding like wipe, you can google for info on that

You can also shred a file using bash scripts but in this post we’ll use python, so if you are a python student or python enthusiast and you’d like to learn how to shred a file in python or even multiple files using python codes, then read on as we are going to get down to the business of the day.

WHAT YOU NEED TO KNOW ALREADY

To fully understand the following codes, you’d need to be familiar with some basic python concepts like :

Importing and using modules
For-loops
Scope
Function
Basic concepts like printing, fstring, python syntax and other similar stuffs

LET THE CODING BEGIN!

So we’ll begin by importing the necessary modules:

#let's import the modules we need
import glob
import os
import subprocess as sp

Enter fullscreen mode Exit fullscreen mode

In other to successfully shred a file in python we need those imported modules above, however another programmer might also have his/her own way of going about it.

Meanwhile we’ll use glob and os to recursively navigate a given directory and its sub directories and perform our shredding. We’ll then use subprocess to run a command line operations via our python script.

Now let’s start writing our file shredding function:

def shrd():
    file_path = input('Enter the folder path:\n')
Enter fullscreen mode Exit fullscreen mode

So we got the directory path from the user, next we write the code we will use to recursively search the entire directory and sub directory for matching files.

But first let’s see what happens when we don’t specify a specific file type. The code below will print the folders and files in an understandable manner.

gen = glob.iglob(f'{file_path}/**/*', recursive=True)
    for file in gen:
        print(file)
Enter fullscreen mode Exit fullscreen mode

Now let’s write in full a code that checks the entire directory for all the .txt files and shred them:

import subprocess as sp
import os
import glob
def shrd():
    file_path = input('Enter the folder path:\n')
    file_type = input('Enter file type here:\n')
    gen = glob.iglob(f'{file_path}/**/*.{file_type}', recursive=True)
    for file in gen:
        print(f'shredding {file} now\n')
        sp.call(f'shred -zvu -n 10 {file}', shell = True)

        print(f'done shredding {file}, moving to the next file now...\n')

    print('Bravo! All files has been shredded')

shrd()
Enter fullscreen mode Exit fullscreen mode

CODE EXPLAINED:

After getting and saving the directory path and file type in variables, we used glob module to find and delete all the .txt files recursively in the given directory.

We use subprocess to run a command line program(shred) via python script.

Now let me explain the extra arguments -zvu -n in the command:

-z or –zero add a final overwrite with zeros to hide shredding
-u deallocate and remove file after overwriting
-v or –verbose show progress
-n or –iterations is for the overwrite N times instead of the default (3)
To see more arguments options use shred –help in the CLI. Meanwhile we didn’t want to name the function shred to avoid confusion on the code since the we are using the shred pkg, so we used shrd instead.

CONCLUSIONS

I hope you enjoyed the short tutorial and also learnt something new today. If you have questions , please don’t hesitate to ask me in the comment section.

Meanwhile, shred ordinarily shreds one file at a time but our function helps us to shred as many files as we want just by running our code.

Remember you can do this with any other file with another extension other than .txt, like mp3, mp4 and etc.

Now that you have learnt about how to navigate files in a given directory, you can actually do other incredible stuffs like searching for, bulk-delete, move and do other stuffs with files and folders using python.

To see more codes similar to how to shred a file in python kindly check our tutorial post page. You can also check this post if you are interested in seeing code with the glob module or file manipulation codes.

Top comments (0)