Created
February 6, 2022 15:31
-
-
Save vratiu/afdafa01ea836acd9d9479d1d06b0992 to your computer and use it in GitHub Desktop.
Move digit ending jpeg files into subfolders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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