Remove compiled files from directory

I’m running through a bunch of examples and exercises in C++ Primer Plus and I needed a way to remove my compiled scripts from my folder. Attached is a python script which will recursively remove all compiled files from a folder tree.

import os

count = 0;

for (path, dirs, files) in os.walk(os.getcwd()):
	for file in files:
		#check for the presence of a '.'
		if file.find(".") == -1: #character not found
			print "{}/{} was removed".format(path, file);
			os.remove(path+"/"+file)
			count += 1;

print "Done. {} files removed".format(count)

I’m storing all my exercises and examples in a git repository. From what I’ve read, cleaning house with this script and doing an extra commit is easier than hosing everything without an extension using .gitignore.

2 responses to “Remove compiled files from directory”

  1. Paul Clark

    It’s not a problem to ignore all files without an extension with .gitignore — you just have to go about it in reverse. Here’s a .gitignore that I verified does it:


    # Ignore all files
    *

    # Do not ignore files with extensions
    !*.*

    # Do not ignore directories
    !*/

    If there’s something in addition to that your script is doing, you can avoid the extra commit by adding your script to .git/hooks/pre-commit. Make sure to set permissions to 755. It’ll even cancel your commit if something goes wrong if you set the correct exit code.

Leave a Reply