Last active
August 29, 2015 13:57
-
-
Save zet4/9664531 to your computer and use it in GitHub Desktop.
Renames/Moves all files in a folder of execution to a subfolder of choice and then into a subfolder depending on mimetype.
This file contains hidden or 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
''' | |
Description: Renames/Moves all files in a folder of execution to a subfolder of choice and | |
then into a subfolder depending on mimetype, ignores script itself. | |
----- | |
Date: Thursday, 20 March 2014, 07:40:00 CST | |
Author: ZetaHunter | |
Credit: Fellow redditors SatanKidneyPie and vmsmith. | |
''' | |
import os | |
import mimetypes | |
take = os.path.dirname(os.path.abspath(__file__)) | |
print("Hello user, would you like to clean your desktop?") | |
print("Well ofcourse you do, why else would you run this script, now then,") | |
home = os.path.join(take, input("Please, enter a name for the target folder:")); | |
print("Cleaning", take) | |
#Make 'home' if none exists directory. | |
if not os.path.isdir(home): | |
os.mkdir(home) | |
def process(file): | |
""" | |
Do the work on file, select correct folder, and move to it. | |
""" | |
ext, _ = mimetypes.guess_type(os.path.abspath(file)) | |
if ext is None: | |
ext = "unknown" | |
else: | |
ext, second = ext.split('/') | |
if ext == "application": | |
ext = os.path.splitext(file)[1] | |
target = os.path.join(home, ext, file) | |
if not os.path.isdir(os.path.dirname(target)): | |
os.mkdir(os.path.dirname(target)) | |
if not os.path.isfile(target): | |
os.rename(file, target) | |
print(file, "Moved to " + target) | |
[process(f) for f in os.listdir(take) if (not os.path.isdir(f)) and (f != os.path.basename(__file__))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment