[kwlug-disc] Fwd: Regular Expression to Match Movie Titles and Year and Ignore the rest.
Jason Eckert
jason.eckert at gmail.com
Wed Jan 1 09:23:07 EST 2025
If you're using Python, you could just use regular expressions to provide
for the new filename.
For example,
pattern =
r"^([A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*)\.(\d{4})\..*?(\d+p)\..*\.mp4$"
replacement = r"\1 \2-\3.mp4"
You'd have to import re and os, of course - but then you could use a for
loop to process each file.
For example (keeping it just to mp4s for simplicity):
def rename_movie_files(file_list):
for filename in file_list:
if re.match(pattern, filename):
new_filename = re.sub(pattern, replacement, filename)
os.rename(filename, new_filename)
movie_files = [
"Zero.Dark.Thirty.2012.720p.BrRip.x264.BOKUTOX.YIFY.mp4",
"The.Dark.Knight.2008.1080p.BluRay.x264.YIFY.mp4",
"Inception.2010.720p.BluRay.x264.YIFY.mp4"
]
rename_movie_files(movie_files)
On Wed, 1 Jan 2025 at 07:42, Ronald Barnes via kwlug-disc <
kwlug-disc at kwlug.org> wrote:
> John Driezen wrote on 2025-01-01 01:25:
>
> > newfile = movietitle+" ("+year+")-"+resolution+"."+ext
>
> You may be easier for legibility / maintenance to use "f-strings" for
> this formatting & concatenating, à la:
>
> > newfile = f"{movietitle} ({year})-{resolution}.{ext}"
>
>
>
> Just a thought...
>
>
> _______________________________________________
> kwlug-disc mailing list
> To unsubscribe, send an email to kwlug-disc-leave at kwlug.org
> with the subject "unsubscribe", or email
> kwlug-disc-owner at kwlug.org to contact a human being.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20250101/9d6ba6eb/attachment-0001.htm>
More information about the kwlug-disc
mailing list