Welcome, guest | Sign In | My Account | Store | Cart

This command-line program will replace the content of your files with zeros.

Python, 30 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os, sys

def purge(path):
    for name in os.listdir(path):
        path_name = os.path.join(path, name)
        try:
            if os.path.isdir(path_name):
                purge(path_name)
            elif os.path.isfile(path_name):
                size = os.path.getsize(path_name)
                file(path_name, 'wb', 0).write(chr(0) * size)
        except:
            print 'ERROR:', path_name

def main():
    try:
        path = ''
        for index in range(1, len(sys.argv)):
            path += sys.argv[index] + ' '
        path = path[:-1]
        assert os.path.isdir(path)
        purge(path)
        print 'Done.'
    except:
        path = os.path.basename(sys.argv[0])
        path = path[:path.rfind('.')]
        print path, '<directory>'

if __name__ == '__main__':
    main()

If you want to get rid of the contents of your files before you delete them, just use this program for the job.

1 comment

Stephen Chappell (author) 18 years, 4 months ago  # | flag

Version 2.

import os, sys

def main():
    try:
        purge(' '.join(argv[1:])
        print 'Done.'
    except:
        print os.path.basename(sys.argv[0]), ''

def purge(path):
    for name in os.listdir(path):
        path_name = os.path.join(path, name)
        try:
            if os.path.isdir(path_name):
                purge(path_name)
            elif os.path.isfile(path_name):
                size = os.path.getsize(path_name)
                file(path_name, 'wb', 0).write(chr(0) * size)
        except:
            print 'ERROR:', path_name

if __name__ == '__main__':
    main()