批量删除文件名中的字符 [英] Removing characters from filename in batch
问题描述
我在Windows资源管理器中有3个主文件夹,其中包含诸如ALB_01_00000_intsect_d.kml或Baxters_Creek_AL_intsect_d.kml这样的命名文件.即使名字更改了,我也希望从所有这些文件中删除的一致名称是"_intsect_d".想要对每个文件夹中的所有文件执行此操作.这些文件的扩展名为.kml.根据以上示例,我期望的结果是ALB_01_00000.kml,而另一个将是Baxters_Creek_AL.kml.对python编程了解不多,但是希望帮助编写一个可以实现上述结果的脚本.谢谢
I have 3 main folder in Windows explorer that contain files with naming like this ALB_01_00000_intsect_d.kml or Baxters_Creek_AL_intsect_d.kml. Even though the first name changes the consistent thing that I would like to remove from all these files is "_intsect_d". Would like to do this for all files within each of the folders. The files have an extension .kml. The result I am expecting as per the example above is ALB_01_00000.kml and the other one would be Baxters_Creek_AL.kml. Dont know much about programming in python, but would like help to write a script that can acheive the result mentioned above. Thanks
推荐答案
import os
for filename in os.listdir('dirname'):
os.rename(filename, filename.replace('_intsect_d', ''))
这篇关于批量删除文件名中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!