Skip to content

Instantly share code, notes, and snippets.

@sdhutchins
Last active June 2, 2017 23:28
Show Gist options
  • Save sdhutchins/65c861404a699bb046608b4cd8dad4d4 to your computer and use it in GitHub Desktop.
Save sdhutchins/65c861404a699bb046608b4cd8dad4d4 to your computer and use it in GitHub Desktop.
Delete desired directories from root directories
"""Random Utilities"""
import shutil
import os
import subprocess
class StyleFix:
"""Perform automatic pep8 for code in a top down fashion.
pip install autope8 to use this.
"""
def __init__(self, include, dirpath):
# Directories to exclude
include = [include]
for root, dirs, files in os.walk(dirpath, topdown=True):
dirs[:] = [d for d in dirs if d in include]
for name in files:
if name.endswith('.py'):
pep8cmd = "autopep8 --in-place --aggressive " + str(os.path.join(root, name))
os.system(pep8cmd)
print(pep8cmd)
callcmd = subprocess.check_output(pep8cmd, shell=True).decode()
if callcmd == 0: # Command was successful.
print('Autopep8 was performed on %s' % name)
else:
print('Failed autopep8 on %s' % name)
class DeleteDir:
"""Delete a certain directory from all sub directories.
This function uses os.walk to walk a top level directory and delete pycache
directories in each subdirectory.
"""
def __init__(self, rootpath, directoryname):
r = rootpath
# Directories to exclude
delete = [directoryname]
for root, dirs, files in os.walk(r, topdown=True):
for directory in dirs:
if directory in delete:
dirpath = os.path.join(root, directory)
try:
shutil.rmtree(dirpath)
print("The following directory was deleted: %s" % dirpath)
except:
raise OSError
class Delcache(DeleteDir):
"""Delete pycache directories."""
def __init__(self, rootpath=r'C:\Users\shutchins2\Desktop\GitRepo',
directoryname='__pycache__'):
super(Delcache, self).__init__(rootpath, directoryname)
@sdhutchins
Copy link
Author

I mainly use this to delete pycache directories.

Example

delete_directory(rootdirectory, '__pycache__')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment