From 68813174cee188a97effee30b318936b05eaa8f2 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Fri, 25 Nov 2022 22:57:27 -0600 Subject: [PATCH] Added folderify.py --- README.md | 2 +- folderify.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 folderify.py diff --git a/README.md b/README.md index 535e9e3..86bc281 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Number of scripts](https://img.shields.io/badge/number_of_scripts-31-blue) +![Number of scripts](https://img.shields.io/badge/number_of_scripts-32-blue) # Random Scripts This repository is for random scripts I wrote mostly for personal use. diff --git a/folderify.py b/folderify.py new file mode 100644 index 0000000..1912959 --- /dev/null +++ b/folderify.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +# This is a simple script to find all video files in a folder and move them into their own folders. +# It also detects .srt subtitle files with matching names and moves them into their corresponding +# video file's folder. + +import os +from pathlib import Path + +path = os.getcwd() +files = (file for file in os.listdir(path) + if os.path.isfile(file) and Path(file).suffix in ['.mkv', '.mp4', '.avi', '.3gp']) +ctr = 0 + +for x in files: + filename = Path(x) + basename = str(filename.with_suffix('')) + if not os.path.isdir(basename): + os.mkdir(basename) + else: + print('The folder '+basename + + ' already exists, please deal with that file manually.') + continue + os.rename(path+"/"+x, path+"/"+basename+"/"+x) + if os.path.isfile(basename+'.srt'): + os.rename(path+'/'+basename+'.srt', path + + '/'+basename+'/'+basename+'.srt') + ctr += 1 + +print(str(ctr)+' folder(s) created.')