How to remove files using Python (File Remover program included)

1. Problem Description:

After doing some recovery from hard drive the software has created thousand of files which I didn’t needed.

Around 200k txt files . Removing that huge number of files using Windows is very painful because the Windows Explorer crashed. Bill Gates can’t handle it!!!

So I have write some basic script to do it for me:

2. Program (use it at your own risk )

Program goes through  all files in specifies directory and remove specific file types eg: .txt – text file.

You can run this program from Python Gui

import os

indir=”H:\\marzena_zdjecia”

def file_remove(extension,indir):
counter = 0
for root, dirs, filenames in os.walk(indir):
#print(root)
#print(dirs)
for f in filenames:
full_path = root + “\\” + f
if f[-3:] == extension:
#print(full_path)
os.remove(full_path)
counter = counter + 1
print(“Removing ” + full_path)
print(“Removing type: ” + extension +” Number of files removed: “+ str(counter))

file_remove(‘txt’,indir)
file_remove(‘sys’,indir)
file_remove(‘swc’,indir)
file_remove(‘html’,indir)
file_remove(‘xml’,indir)
file_remove(‘chm’,indir)
file_remove(‘rtf’,indir)
file_remove(‘dll’,indir)
file_remove(‘cab’,indir)
file_remove(‘lnk’,indir)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s