Created
July 16, 2021 07:27
-
-
Save ehcaning/c7a3450543b2eee3302884dda0b5b252 to your computer and use it in GitHub Desktop.
Create folder with name of files
This file contains hidden or 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
| # 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