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

#法一
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)

posted @ 2018-02-03 15:19  zhchoutai  阅读(191)  评论(0编辑  收藏  举报