如何使用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)