How to Shred Files With Python Code

How to Shred Files With Python Code

You can safely delete data with Python code for shredding files. Here is a small script for how to Shred Files with Python in it with only a few lines. You can customize the functionality of shredder code by adding the number of passes to the shredder function.

This is How you can shred files using Python

Code:

 import os
 file_name = input(‘Enter path for file for shred’)
 f = open(file_name,'wb')
 f.write("00110111110001010100010001001");
 f.close()
 os.rename(file_name,'abf09i')
 os.unlink('abf09i')
 print(file_name + " shredded successfully")

 

HOW IT WORKS:

File Shredding : File shredding is process of completely removing the data and making it unrecoverable.

Data re-write and delete the file with following methods :

file.write() – method rewrite the data of file,
os.rename() – renames the file for completely remove traces
os.unlink() – finaly deletes the file

 

Customized Code :

import os
 file_name = raw_input("Enter path for file for shred ")
 cycles = int(raw_input("cycles for shredding (defaul 1) "))
 def shred():
    data = "01"
    count = 2
    f = open(file_name,'rb+')
   f.read()
   count = file.tell(f)
   data = data*(count/2)
   f.seek(0)
   f.write(data)
   f.close()
 for i in range(0,cycles):
    shred()
 os.rename(file_name,'abf09i')
 os.unlink('abf09i')
 print(file_name + " shredded successfully")