您的位置:首页 > 编程语言 > Python开发

Python批量重命名指定文件夹下文件的两种方法

2015-06-10 06:56 956 查看
#法一
import os
path = "C://Python34//"
for file in os.listdir(path):
if os.path.isfile(os.path.join(path,file))==True:
if file.find('.')<0:
newname=file+'.jpg'
os.rename(os.path.join(path,file),os.path.join(path,newname))

#法二
import os
import glob

path = "C://Python34//"
for infile in glob.glob( os.path.join(path, '*.gif') ):
for i in range (1,21):
index_symbol = "_" + str(i) + "_"
if index_symbol in infile:
newfile = infile
newfile = newfile.replace(index_symbol, str(i));
os.rename(infile, newfile)
print (infile)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息