3

I am trying to make a file renamer using Python. I was able to successfully scrape Wikipedia for list of episodes, but while making the renamer file I was met with a lot of discrepancies. What I want is that instead of '.mkv' at the end I want to use exactly the extension that was matched from the if condition. Is there a way to return it?

extensions = ('.webm','.mkv','.flv','.vob','.ogv', 
  '.ogg','.drc','.gif','.gifv','.mng','.avi','.mov', 
  '.qt','.wmv','.yuv','.rm','.rmvb','.asf','.amv','.mp4',
  '.m4p', '.m4v','.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
  '.mpg', '.mpeg', '.m2v','.m4v','.svi','.3gp','.3g2','.mxf',
  '.roq','.nsv','.f4v', '.f4p', '.f4a' ,'.f4b','.srt')
list = f.readlines()


y = 0
num = 1
for filename in os.listdir(path):
    if filename.endswith(extensions):
      os.rename(path+"\\"+filename,path+"\\"+str(num)+' - '+list[int(y)].strip('\n')+'.mkv') #instead of mkv, I want extension which was matched in the above if condition. 
    y += 1
    num += 1
Ayush Mandowara
  • 490
  • 5
  • 18

3 Answers3

1

Well either you have to loop over the extensions one by one, or you could split the filename to get the extension.

Split by filename

for filename in os.listdir(path):
    if filename.endsswith(extensions):
        extension = filename.split('.')[-1] # you can use os.path.splitext too as Max Chretien suggested
        # ...

Use explicit loop

for filename in os.listdir(path):
    matching_extensions = filter(lambda extension: filename.endswith(extension), extensions)
    if matching_extensions:
        extension = matching_extensions[0]
    # ...
hspandher
  • 15,934
  • 2
  • 32
  • 45
1

Another lead, first I will extract the filename and file_extension using os.path.splitext.

Then if the file_extension matches your extensions tuple I will rename it with a try except to see if there is any errors.

for file_path in os.listdir(path):
    filename, file_extension = os.path.splitext(file_path)
    if file_extension in extensions:
      try:
          os.rename(file_path, filename + '.mkv')
      except OSError:
          print("Error while renaming {}".format(filename))
Kruupös
  • 5,097
  • 3
  • 27
  • 43
0

If I understand your question correctly, probably the following code might work.

extensions = ('.webm','.mkv','.flv','.vob','.ogv', 
  '.ogg','.drc','.gif','.gifv','.mng','.avi','.mov', 
  '.qt','.wmv','.yuv','.rm','.rmvb','.asf','.amv','.mp4',
  '.m4p', '.m4v','.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
  '.mpg', '.mpeg', '.m2v','.m4v','.svi','.3gp','.3g2','.mxf',
  '.roq','.nsv','.f4v', '.f4p', '.f4a' ,'.f4b','.srt')
list = f.readlines()


y = 0
num = 1
for filename in os.listdir(path):
    if ('.'+filename.lower().split('.')[1]) in list(extensions):
        os.rename(path+"\\"+filename,path+"\\"+str(num)+' - '+list[int(y)].strip('\n')+'.mkv')
    y += 1
    num += 1

I just made one minor changes to your code.

from: if filename.endswith(extensions):

to: if ('.'+filename.lower().split('.')[1]) in list(extensions):

Hope it helps!

arnold
  • 598
  • 2
  • 11
  • 24
  • it would be better to use `split('.')[-1]`, as there can be multiple periods in the filename as well. (ex: breaking.bad.s02.e02.mp4) Here, we only want the "mp4" part, and hence, `split(".")[-1]` would do the trick! (Split on period, and get the last element of the list) – Ayush Mandowara Feb 13 '21 at 18:13