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)

评论关闭