python给一个文件夹中的所有文件进行重命名

#!/usr/bin/env python
import os
Root_dir = r"E:\data_rename"
files = os.listdir(Root_dir)
print(files)	#list型
print(type(files))
count = 1
for file in files:
	newname_token = []
	newname_token.append('__')
	newname_token.append(str(count))
	count += 1
	if count <= 4:
		print("newname_token",newname_token)
	newfilename = ''.join(newname_token) + '.txt'
	if count <= 4:
		print("newfilename",newfilename)
	#join的作用是将list连接成一个字符串, list=[1,2,3], "-".join(list) => 1-2-3
	old_path = os.path.join(Root_dir,file)
	new_path = os.path.join(Root_dir,newfilename)
	os.rename(old_path,new_path)
发布了97 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_32460819/article/details/102409888
今日推荐