我下载文件名很长的播客,然后我将它们剥离,以便它们只有城市名称、日期和时间(意味着第一、第二或第三个小时)。除了 os.rename(file, new_name) 之外,这一切似乎都有效,它告诉我 Windows 无法访问该文件。
import re, os, glob
from ID3 import *
for files in glob.glob("f:\\Download\*podcasts*"):
  os.chdir(files)
  for file in os.listdir("."):
   if re.search("\A[1-3].",file):  # original filenames begin with 1.,2. or 3.
      tags=ID3(file)
      date = re.search("\w*.-\w*.-\w*.",file) # filenames contain date in MMM-DD-YYYY
      date_clean = date.group(0).strip()
      hour = re.search("hr\d", file)  # file names also contain hr. 1,2 or 3 at end
      hour_clean = hour.group(0).strip()
      tags['ARTIST'] = "Portland Podcast"
      tags['TITLE'] = date_clean + hour_clean
      new_name = "Portland-" + date_clean + "-" + hour_clean +".mp3"
      print "Changing",file,"to",new_name+"."
      os.rename(file,new_name)               
  os.chdir("F:\\Download")                           
  os.getcwd()                                        
  os.system("pause")