simplify os check

master
piyx 4 years ago committed by piyx
parent 13a4d76806
commit 2cddb10905

@ -9,52 +9,49 @@ FILE_NAME = "organize.py"
EXT_NAME = "ext.py" EXT_NAME = "ext.py"
def organize_files(drive): def organize_files(path):
# check os
operating_system = platform.system() operating_system = platform.system()
if operating_system == "Windows": # Windows if operating_system == "Windows": # Windows
drive = drive + "\\" path = path + "\\"
elif operating_system == "Darwin": # Mac elif operating_system == "Darwin": # Mac
drive = os.path.expanduser(drive) path = os.path.expanduser(path)
else: else:
print(f"Operating system {operating_system} not currently supported") print(f"Operating system {operating_system} not currently supported")
sys.exit(0) sys.exit(0)
if not os.path.exists(drive): if not os.path.exists(path):
print(f"ERROR! {drive} is not a valid location") print("ERROR! Invalid location")
return return
files = os.listdir(drive) files = os.listdir(path)
extns = {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(drive + folder): if folder and not os.path.exists(path + folder):
os.makedirs(drive + folder) os.makedirs(path + folder)
# Move Files To Folders # Move Files To Folders
for file in files: for file in files:
if file in [FILE_NAME, EXT_NAME]: if file in [FILE_NAME, EXT_NAME]:
continue continue
ext = os.path.splitext(file)[1].strip(".") ext = os.path.splitext(file)[1].strip('.')
folder = foldername(ext) folder = foldername(ext)
if not folder: if not folder:
continue continue
src = drive + file src = path + file
dest = drive + folder + "/" + file dest = path + 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 {drive}") print(f"\nSUCCESS! All files organized in {path}")
if __name__ == "__main__": try:
try: location = sys.argv[1]
# location = sys.argv[1] organize_files(location)
# organize_files(location) except Exception as e:
organize_files('~/Trash') print(f"error {e}")
except Exception as e:
print("USAGE: python organize.py <location>") print("USAGE: python organize.py <location>")
print(e)

Loading…
Cancel
Save