Last active
September 24, 2015 15:21
-
-
Save michaeltrainor/f2a849dd0051d35bc2ba to your computer and use it in GitHub Desktop.
This is a convenience function that deletes all items in a given path and the path itself. The use of 'robocopy' ensures that paths that exceed the 255 character limitation are removed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# standard libraries | |
import os | |
import sys | |
import subprocess | |
__author__ = "Michael Trainor (http://www.mtrainor.com)" | |
__gist__ = "git@gist.github.com:f2a849dd0051d35bc2ba.git" | |
# usage | |
""" python.exe delete_all.py {your_path} """ | |
def clean_path(path): | |
""" | |
Cleans up path strings by normalizing slashing and stripping whitespace | |
:param path: valid system string path | |
""" | |
return path.replace("\\", "/").replace("//", "/").strip() | |
def delete_all(path): | |
""" | |
This is a convenience function that deletes all items in a given path and the path itself. The | |
use of 'robocopy' ensures that paths that exceed the 255 character limitation are removed. | |
:param path: valid system string path | |
""" | |
try: | |
# handle pathing | |
base_path = os.path.split(clean_path(path))[0] | |
mir_path = clean_path(os.path.join(base_path, "mir")) | |
if not os.path.exists(mir_path): | |
os.makedirs(mir_path) | |
# robocopy process | |
command = "robocopy {0} {1} /mir".format(mir_path, clean_path(path)) | |
process = subprocess.Popen(command, shell=False) | |
process.wait() | |
# cleanup | |
if os.path.exists(path): | |
os.rmdir(path) | |
if os.path.exists(mir_path): | |
os.rmdir(mir_path) | |
except (WindowsError, IOError, TypeError) as err: | |
raise err | |
if __name__ == "__main__": | |
if os.path.exists(sys.argv[1]): | |
delete_all(sys.argv[1]) | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment