Skip to content

Instantly share code, notes, and snippets.

@ehcaning
Created July 16, 2021 07:27
Show Gist options
  • Select an option

  • Save ehcaning/c7a3450543b2eee3302884dda0b5b252 to your computer and use it in GitHub Desktop.

Select an option

Save ehcaning/c7a3450543b2eee3302884dda0b5b252 to your computer and use it in GitHub Desktop.
Create folder with name of files
# Create a folder for this file:
# Thor.The.Dark.World.2013.1080p.6CH.x265.HEVC.mkv
# With this name:
# Thor The Dark World (2013)
# And put movie in created folder
import re
import os
import shutil
cwd = os.getcwd()
regex = r"(^.*)(2\d{3})"
def move(file_name, folder_name):
shutil.move(os.path.join(cwd, file_name), os.path.join(cwd, folder_name, file_name))
files = [f for f in os.listdir(".") if os.path.isfile(f)]
for file in files:
matches = re.findall(regex, file)
if len(matches) == 0:
continue
matches = matches[0]
movie_name, movie_year = matches[0], matches[1]
movie_name = movie_name.replace("(", "").replace(".", " ")
movie_name = movie_name.strip()
folder_name = f"{movie_name} ({movie_year})"
print(folder_name)
os.makedirs(folder_name)
move(file, folder_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment