add mac functionality

master
piyx 4 years ago committed by piyx
parent a372e4e5d6
commit 7fe41c3751

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

Loading…
Cancel
Save