因为需要将 pdf 文件转换为电子书格式 epub,但是转换后的文件名是 (NEW)炒菜.epub,多了个 (NEW),现在想用 python 批量将 (NEW) 删除。
下面是我做的例子:
re.py
#!/usr/bin/python
import os,sys
path="C:\\huyunan\\learning\\python\\test"
def rename(path):
for name in os.listdir(path):
if(name.startswith("(NEW)") and name.endswith(".epub")):
print(name)
newName = name.replace("(NEW)", "")
os.rename(os.path.join(path, name), os.path.join(path, newName))
rename(path)
os.listdir(path):列出目录下所有文件并遍历循环
name.startswith("(NEW)"):判断文件名是否以 "(NEW)") 开关
name.endswith(".epub"):判断文件名是否以 ".epub" 结尾
name.replace("(NEW)", ""):文件名替换
os.path.join(path, newName):获取新文件名路径
os.rename(os.path.join(path, name), os.path.join(path, newName)):重命名文件