【问题标题】:My python script, which recursively renames files, fails我的 python 脚本,它递归地重命名文件,失败了
【发布时间】:2017-12-24 12:52:38
【问题描述】:

我正在尝试递归重命名目录中的文件,因此我编写了一个 python 脚本来处理重命名。所以理想情况下,脚本应该可以转这个

Nicholass-MacBook-Air-2:RenameXtalTest nick$ ls
RM01_03_000_0213_Proj1_Clon1_RC_0000RC000870_010_171222_01_03_02_E0_00_031_001_RAI.jpg
RM01_03_000_0213_Proj1_Clon1_RC_0000RC000870_010_171222_07_07_01_E0_99_031_001_RAI.jpg
rename.py

进入

Nicholass-MacBook-Air-2:RenameXtalTest nick$ ls
RC000870-C1_02-E0_00.jpg
RC000870-G7_01-E0_99.jpg
rename.py

如果看起来像这样,我可以让 rename.py 处理 Python 中的单个名称

#!/usr/bin/python
oldname = 'RM01_03_000_0213_Proj1_Clon1_RC_0000RC000870_010_171222_01_03_02_E0_00_031_001_RAI.jpg'  #manually set an old name
rowdic = {"01" : "A", "02" : "B", "03" : "C", "04" : "D", "05" : "E", "06" : "F", "07" : "G", "08" : "H"} #dictionary to translate oldname char 59 to 61
coldic = {"01" : "1", "02" : "2", "03" : "3", "04" : "4", "05" : "5", "06" : "6", "07" : "7", "08" : "8", "09" : "9", "10" : "10", "11" : "11", "12" : "12"} #dictionary to translate oldname char 56 to 58
subwdic = {"01" : "1", "02" : "2", "03" : "3"} #dictionary to translate oldname char 62 to 64

newname = oldname[36:44]+"-"+rowdic[oldname[59:61]]+coldic[oldname[56:58]]+"_"+subwdic[oldname[62:64]]+"-"+oldname[65:70]+".jpg" #definition for how to shorten, rearrange, and swap out some parts of oldname

print newname

但一旦我尝试让脚本在 Unix 中的多个文件上运行 - 它就会失败。这是我对上述内容的脚本尝试:

#!/usr/bin/python
import os
rowdic = {"01" : "A", "02" : "B", "03" : "C", "04" : "D", "05" : "E", "06" : "F", "07" : "G", "08" : "H"} #dictionary to translate oldname char 59 to 61
coldic = {"01" : "1", "02" : "2", "03" : "3", "04" : "4", "05" : "5", "06" : "6", "07" : "7", "08" : "8", "09" : "9", "10" : "10", "11" : "11", "12" : "12"} #dictionary to translate oldname char 56 to 58
subwdic = {"01" : "1", "02" : "2", "03" : "3"} #dictionary to translate oldname char 62 to 64
for oldname in os.listdir("."): #get list of all oldnames in current directory
    newname = oldname[36:44]+"-"+rowdic[oldname[59:61]]+coldic[oldname[56:58]]+"_"+subwdic[oldname[62:64]]+"-"+oldname[65:70]+".jpg" #definition for how to shorten, rearrange, and swap out some parts of oldname
    os.rename(oldname, newname) #command to change all old names to new names

这是我尝试运行它时遇到的错误:

Nicholass-MacBook-Air-2:RenameXtalTest nick$ ./rename.py 
Traceback (most recent call last):
  File "./rename.py", line 7, in <module>
    newname = oldname[36:44]+"-"+rowdic[oldname[59:61]]+coldic[oldname[56:58]]+"_"+subwdic[oldname[62:64]]+"-"+oldname[65:70]+".jpg"
KeyError: ''

谁能帮我理解错误的含义,以及我可以做些什么来修复它?

【问题讨论】:

  • 看起来您不处理不是该特定格式的文件名。尝试打印oldname 的值。除了其他文件之外,它可能还会为您提供 ...rename.py
  • 是的,正如@mooiamaduck 所说。虽然这不是最佳解决方案,但您可以简单地在循环主体中包含类似 if len(oldname) &gt; 70: 的 if 语句,以便仅对超过 70 个字符的文件名执行处理。

标签: python dictionary for-loop subprocess substring


【解决方案1】:

@mooiamaduck 和我的 cmets 的组合给出了以下代码:

for oldname in os.listdir("."):
    if len(oldname) < 70:
        continue 
    newname = oldname[36:44]+"-"+rowdic[oldname[59:61]]+coldic[oldname[56:58]]+"_"+subwdic[oldname[62:64]]+"-"+oldname[65:70]+".jpg"
    os.rename(oldname, newname) #command to change all old names to new names

问题基本上是您还在处理不具有您希望重命名的特定文件名格式的文件。 (例如您的...rename.py。)例如oldname[62:64] 然后转换为一个空字符串,然后您将其用作subwdic 的键,从而产生空字符串KeyError

这里提出的简单解决方法是仅考虑包含至少 70 个字符的文件名。更好的解决方案当然是对文件名进行适当的检查以查看它是否与您的模式匹配,但这对于您的特定用例来说很好。

【讨论】:

  • 是的,它成功了。谢谢你
  • Glenn Codes,如果文件名中包含“.jpg”,那么仅处理文件名的好声明是什么?
  • 另外,你的最后一段代码声明了“if len(oldname) 70”,脚本会再次失败。为什么?
  • 如果if 语句的计算结果为True,它将continue,这意味着它将跳过循环体的其余部分并继续到下一个文件。
  • 要检查文件名是否以.jpg 结尾,您可以使用oldname.endswith(".jpg")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 2018-11-28
相关资源
最近更新 更多