我很难使用os.rename批量重命名文件

时间:2018-09-24 00:26:30

标签: python

我的代码:

import os

videos = {}
subtitles = {}

files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file in files:
 if (file[-3:] == "ass" or file[-3:] == "srt"):
  episode = file.split('#')[1].split(' ')[0]
  subtitles[episode] = file

 if (file[-3:] == "mp4"):
  episode = str(int(file.split('ep')[1].split('.')[0]))
  videos[episode] = file


i = 1
for sub in subtitles:
 try:
  extension = str(subtitles.get(str(i)))[-3:]
  video = str(videos.get(str(i))).split('.')[0]+"."+extension
  subtitle =  str(subtitles.get(str(i)))
  print (subtitle + video)
  os.rename(subtitle, video) # <---- 
  i+= 1
 except Exception as e:
  print ("Error: " + sub + " exception: " + str(e))

如果我注释“ os.rename(subtitle,video)”行,仅保留打印命令,它将正常打印所有文件名,但是如果不注释,则所有文件名将变为“ None” ,给我每个文件的错误:

NoneNone.one
Error: 99 exception: [WinError 2] The system cannot find the file specified: 'None' -> 'None.one'

0 个答案:

没有答案