Python文件重命名脚本

时间:2017-10-01 21:47:49

标签: python

我正试图从我60首歌曲的专辑中删除“(歌词_抒情视频)”。

import os

toBeRemoved="(Lyrics _ Lyric Video) "
path=r'E:\Users\Dylan\Desktop\MediaHuman2'
directory = os.listdir(os.fsencode(path))

for file in directory:
    filename = os.fsdecode(file)
    if toBeRemoved in filename:
        newname=filename.replace(toBeRemoved, "")
        os.rename(path+filename, path+newname)
    else:
        continue

但我一直得到这个:

    Traceback (most recent call last):
  File "E:\Users\Dylan\Desktop\filerenamer.py", line 11, in <module>
    os.rename(path+filename, path+newname)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'E:\\Users\\Dylan\\Desktop\\MediaHuman202 - NOTD - Summer of Love (Lyrics _ Lyric Video) Ft. Dagny.mp3' -> 'E:\\Users\\Dylan\\Desktop\\MediaHuman202 - NOTD - Summer of Love Ft. Dagny.mp3'

编辑:文件确实存在

3 个答案:

答案 0 :(得分:4)

使用os.path.join(path, filename)以使用正确分隔符的方式连接文件名。

for file in directory:
    filename = os.fsdecode(file)
    if toBeRemoved in filename:
        newname=filename.replace(toBeRemoved, "")
        os.rename(os.path.join(path, filename), os.path.join(path, newname))

答案 1 :(得分:1)

尝试

path=r'E:\Users\Dylan\Desktop\MediaHuman2''\\'

而不是

path=r'E:\Users\Dylan\Desktop\MediaHuman2'

答案 2 :(得分:0)

试试这段代码。它不是最好的解决方案,但它会起作用。 如果符合您的需求,请告诉我。

使用功能:

path =  r'E:\NN_Music\mask rename\testing - Copy\input'
MaskRename(path, "Lyrics")

输入:

  1. tmp1.txt
  2. tmp2.txt
  3. tmp3.txt
  4. 输出:

    1. Lyric_0.txt
    2. Lyric_1.txt
    3. Lyric_3.txt
    4. 代码:

      import os
      import shutil
      import operator
      
      def diff(first, second):
              second = set(second)
              return [item for item in first if item not in second]
      
      def MaskRename(target_path, mask):
          namesOfFiles = os.listdir(target_path)
          i = 1
          l = list()
          #removing extension
          for item in namesOfFiles:
              l.append(os.path.splitext(item)[0])
      
          futureNamesOfFiles = list()
          #applying new mask 
          for q in range(0, len(l),1):
              futureNamesOfFiles.append(mask + "_" + str(q))  
      
          listOfDiff = diff(l, futureNamesOfFiles)
      
          fileAfterRename = [ x for x in namesOfFiles if x.startswith(tuple(listOfDiff)) ]
      
          iterator = 0
          shift = 1
          for newItem in fileAfterRename:
              filename, file_extension = os.path.splitext(newItem)
              old_file = os.path.join(target_path, newItem)
              new_file = os.path.join(target_path, futureNamesOfFiles[iterator] + file_extension)
              try:
                  os.rename(old_file, new_file)
                  iterator = iterator + 1
              except Exception:
                  new_file = os.path.join(target_path, futureNamesOfFiles[iterator + 1] + file_extension)
                  os.rename(old_file, new_file)
                  iterator = iterator + 2
                  pass