Skip to content

Instantly share code, notes, and snippets.

@firedynasty
Created February 22, 2023 06:15
Show Gist options
  • Save firedynasty/5df5cccc9eb41621e7eac679d1c14a16 to your computer and use it in GitHub Desktop.
Save firedynasty/5df5cccc9eb41621e7eac679d1c14a16 to your computer and use it in GitHub Desktop.
file mover with Python
import os
import shutil
import random
sourcepath = "/Users/name/screenshots"
destpath = "/Users/name/"
file_names = os.listdir(sourcepath)
file_dict = {
"hello" : "documents/linkedin/projects/file-sorter",
"loc1": "documents/linkedin/job_listings/taken_screenshots"
}
#if the file has hello in it, will move to documents/linkedin/projects/file-sorter
# Generate a random 4-digit number
random_number = random.randint(1000, 9999)
for file in file_names:
for keyword, folder in file_dict.items():
if keyword in file:
# Add the random number to the file name
new_file_name = f"{random_number}_{file}"
# Move the file with the new name
shutil.move(os.path.join(sourcepath, file), os.path.join(destpath, folder, new_file_name))
# Print the confirmation message
print(f"Moved {file} to {os.path.join(destpath, folder, new_file_name)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment