0

我下载文件名很长的播客,然后我将它们剥离,以便它们只有城市名称、日期和时间(意味着第一、第二或第三个小时)。除了 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")
4

1 回答 1

0

某物持有对您尝试移动的文件的引用。在 周围放置一个try/exceptshutil.rename并打印出导致问题的文件。当您找出导致问题的文件时,找出谁持有对它的引用。

查找谁持有文件引用的一种方法是使用Process ExplorerUnlocker

更新:

我查看了 ID3 库的代码,似乎如果你传入一个文件名,它不会关闭文件,永远:D

您应该执行以下操作:

with open(filename, 'rb') as f:
    tags = ID3(f)
    # do your stuff
shutil.rename(filename, ...)
于 2013-08-19T23:46:08.147 回答