python批量修改文件程序代码

本文介绍了如何使用Python的os模块和字符串方法批量修改文件名,移除文件名中的数字。通过os.listdir获取目录内容,os.rename进行重命名,以及str.translate配合str.maketrans删除指定字符。在实践中遇到os.getcwd()获取当前工作目录导致的问题,并提供相关Python官方API链接作为参考。
摘要由CSDN通过智能技术生成

1:首先我们来介绍几个函数
os.listdir(path);会得到这个给定的目录中的所有东西——文件和目录
os是python内置的模块叫做os操作系统(operating system)的简称。
rename(src,dst):给文件或文件夹改名(可以改路径,但是不能覆盖目标文件)
string.translate()requires a dict that maps unicode ordinals to other unicode oridinals (or None if you want to remove the character).
例如:

old_string = "file52.txt"
to_remove = "0123456789"
table = {ord(char): None for char in to_remove}
new_string = old_string.translate(table)
assert new_string == "file.txt"

However, there is simpler way of making a table though, by using the str.maketran

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值