[kwlug-disc] Fwd: Regular Expression to Match Movie Titles and Year and Ignore the rest.

John Driezen jdriezen at sympatico.ca
Wed Jan 1 04:25:23 EST 2025


OK.  I managed to get a working python script to accomplish the intended 
task.  Here it is:

import os
import re

LOGFILE = "/home/john/logs/rename.log"
logfile = open(LOGFILE, 'w')

with os.scandir() as i:
     for entry in i:
         if entry.is_file():
             ## Use named groups for easy use of captured data:
             ## Also, put it all in one regex for easy positional 
grabbing of
             ## elements:
             ## Finally, wrapped for legibility in email, etc.:
             oldfile = entry.name
             print(oldfile)
             info = re.search(
                 "^"
                 + "(?P<title>.*)"                  # capture movie title
                 + "\.(?P<year>\d{4})\."      # capture year
                 + "(?P<res>\d+p)"              # capture resolution
                 + ".*\.(?P<ext>.*)"              # capture filename 
extension
                 + "$",
                 oldfile)
             title = info.group('title')
             movietitle = title.replace('.', ' ') # convert periods to 
spaces
             year = info.group('year')
             resolution = info.group('res')
             ext = info.group('ext')
             if (movietitle and year and resolution and ext): # do not 
write new filename if any values are None
                 newfile = movietitle+" ("+year+")-"+resolution+"."+ext
                 print (newfile)
                 #os.rename(oldfile, newfile)    # rename movie file
                 #os.chmod(newfile , 0o644)      # set permissions on 
movie file
                 log_message = oldfile + " renamed as " + newfile + "\n"
                 print (log_message)
                 logfile.write(log_message)

logfile.close()

Thank you to all for suggestions.


On 2025-01-01 12:42 a.m., John Driezen wrote:
>
> On 2024-12-31 11:36 p.m., Ronald Barnes via kwlug-disc wrote:
>> John Driezen wrote on 2024-12-31 15:29:
>>
>>> This program gives the following error messages when run.
>>
>> Which version of Python?
>>
> Python 3.12.3
>
>> I ran it through 3.10 and got nothing like that.
>>
>>
>> > Suggestions and improvements welcome.
>>
>> Try this:
>>
>> import os
>> import re
>> with os.scandir() as i:
>>   for entry in i:
>>     if entry.is_file():
>>       ## Use named groups for easy use of captured data:
>>       ## Also, put it all in one regex for easy positional grabbing of
>>       ## elements:
>>       ## Finally, wrapped for legibility in email, etc.:
>>       info = re.search(
>>         "^"
>>         + "(?P<title>.*)"
>>         + "\.(?P<year>\d{4})\."
>>         + "(?P<res>\d+p)"
>>         + ".*\.(?P<ext>.*)"
>>         + "$",
>>         entry.name)
>>       print( f"title: {info.group('title')}")
>>       print( f"year: {info.group('year')}")
>>       print( f"resolution: {info.group('res')}")
>>       print( f"extension: {info.group('ext')}")
>>
>>
>> Output:
>>
>> title: Zero.Dark.Thirty
>> year: 2012
>> resolution: 720p
>> extension: mp4
>>
> Thank you.  I will try that code.
>
> _______________________________________________
> 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.



More information about the kwlug-disc mailing list