如何使用Python将文本添加到多个文件名? [英] How do I add text to multiple filenames using Python?

查看:68
本文介绍了如何使用Python将文本添加到多个文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列文件,需要在文件名的末尾添加创建年份(2007年):

I have a series of files that I need to add the creation year (2007) to the end of the filename:

当前:NewZealand_cities.shpNewZealand_roads.shp等

Currently: NewZealand_cities.shp NewZealand_roads.shp etc.

需要:新西兰城市2007.shpNewZealand_roads2007.shp

Need: NewZealand_cities2007.shp NewZealand_roads2007.shp

我已经能够删除文本段,但由于某种原因无法添加.任何帮助将非常感激.谢谢.

I have been able to remove segments of text but cannot add for some reason. Any help would be much appreciated. Thanks.

推荐答案

您尝试过以下方法:

import os
name, ext = os.path.splitext(fname)
os.rename(fname, name + '2007' + ext)

这篇关于如何使用Python将文本添加到多个文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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