|
|
@ -1,25 +1,62 @@
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import platform
|
|
|
|
import shutil
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
from ext import foldername
|
|
|
|
from ext import foldername
|
|
|
|
|
|
|
|
|
|
|
|
FILE_NAME = "organize.py"
|
|
|
|
FILE_NAME = "organize.py"
|
|
|
|
EXT_NAME = "ext.py"
|
|
|
|
EXT_NAME = "ext.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def organize_files(path):
|
|
|
|
def organize_files(drive):
|
|
|
|
path = path+'\\'
|
|
|
|
# check os
|
|
|
|
if not os.path.exists(path):
|
|
|
|
operating_system = platform.system()
|
|
|
|
|
|
|
|
if operating_system == 'Darwin': # Mac
|
|
|
|
|
|
|
|
# drive = os.path.join(drive, '/')
|
|
|
|
|
|
|
|
drive = os.path.expanduser(drive)
|
|
|
|
|
|
|
|
if not os.path.exists(drive):
|
|
|
|
|
|
|
|
print(f"ERROR! {drive} is not a valid location")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
files = os.listdir(drive)
|
|
|
|
|
|
|
|
extns = {os.path.splitext(file)[1].strip('.') for file in files}
|
|
|
|
|
|
|
|
# Create Folders
|
|
|
|
|
|
|
|
for ext in extns:
|
|
|
|
|
|
|
|
folder = foldername(ext)
|
|
|
|
|
|
|
|
if folder and not os.path.exists(os.path.join(drive, folder)):
|
|
|
|
|
|
|
|
os.makedirs(os.path.join(drive, folder))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Move Files To Folders
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
|
|
|
|
if file in [FILE_NAME, EXT_NAME]:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
ext = os.path.splitext(file)[1].strip('.')
|
|
|
|
|
|
|
|
folder = foldername(ext)
|
|
|
|
|
|
|
|
if not folder:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src = os.path.join(drive, file)
|
|
|
|
|
|
|
|
dest = os.path.join(drive, folder, file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
|
|
|
|
shutil.move(src, dest)
|
|
|
|
|
|
|
|
print(f"Moved {file} to {folder}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\nSUCCESS! All files organized in {drive}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif operating_system == 'Windows': # Windows
|
|
|
|
|
|
|
|
drive = drive + '\\'
|
|
|
|
|
|
|
|
if not os.path.exists(drive):
|
|
|
|
print("ERROR! Invalid location")
|
|
|
|
print("ERROR! Invalid location")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
files = os.listdir(path)
|
|
|
|
files = os.listdir(drive)
|
|
|
|
extns = set([os.path.splitext(file)[1].strip('.') for file in files])
|
|
|
|
extns = {os.path.splitext(file)[1].strip('.') for file in files}
|
|
|
|
|
|
|
|
|
|
|
|
# Create Folders
|
|
|
|
# Create Folders
|
|
|
|
for ext in extns:
|
|
|
|
for ext in extns:
|
|
|
|
folder = foldername(ext)
|
|
|
|
folder = foldername(ext)
|
|
|
|
if folder and not os.path.exists(path+folder):
|
|
|
|
if folder and not os.path.exists(drive + folder):
|
|
|
|
os.makedirs(path+folder)
|
|
|
|
os.makedirs(drive + folder)
|
|
|
|
|
|
|
|
|
|
|
|
# Move Files To Folders
|
|
|
|
# Move Files To Folders
|
|
|
|
for file in files:
|
|
|
|
for file in files:
|
|
|
@ -30,18 +67,20 @@ def organize_files(path):
|
|
|
|
if not folder:
|
|
|
|
if not folder:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
src = path+file
|
|
|
|
src = drive + file
|
|
|
|
dest = path+folder+'/'+file
|
|
|
|
dest = drive + folder + '/' + file
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
shutil.move(src, dest)
|
|
|
|
shutil.move(src, dest)
|
|
|
|
print(f"Moved {file} to {folder}")
|
|
|
|
print(f"Moved {file} to {folder}")
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\nSUCCESS! All files organized in {path}")
|
|
|
|
print(f"\nSUCCESS! All files organized in {drive}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
try:
|
|
|
|
location = sys.argv[1]
|
|
|
|
location = sys.argv[1]
|
|
|
|
organize_files(location)
|
|
|
|
organize_files(location)
|
|
|
|
except Exception:
|
|
|
|
except Exception as e:
|
|
|
|
print("USAGE: python organize.py <location>")
|
|
|
|
print("USAGE: python organize.py <location>")
|
|
|
|
|
|
|
|
print(e)
|
|
|
|