批量删除文件名中的字符 [英] Removing characters from filename in batch

查看:83
本文介绍了批量删除文件名中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆