Skip to content

Instantly share code, notes, and snippets.

@vratiu
Created February 6, 2022 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vratiu/afdafa01ea836acd9d9479d1d06b0992 to your computer and use it in GitHub Desktop.
Save vratiu/afdafa01ea836acd9d9479d1d06b0992 to your computer and use it in GitHub Desktop.
Move digit ending jpeg files into subfolders
import os
import re
import shutil
regex = re.compile('^(.*)_\d+\.jpeg$')
dir_path = os.path.dirname(os.path.realpath(__file__))
files = [f for f in os.listdir('.') if (os.path.isfile(f) and regex.match(f))]
dirs = []
for f in files:
dirname = regex.match(f).group(1)
if (dirname not in dirs):
dirs.insert(0, dirname)
print("Creating new directory: ", os.path.join(dir_path, dirname))
os.mkdir(os.path.join(dir_path, dirname))
target = os.path.join(dir_path, dirname, f)
print("Moving file: ", f, " -> ", target)
shutil.move(os.path.join(dir_path, f), target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment