python批量修改文件后缀名

python批量修改文件后缀名

python批量修改文件后缀名

# python批量更换后缀名
import os
import sys
os.chdir(r'F:/test')

# 列出当前目录下所有的文件
files = os.listdir('./')
print('files',files)

for fileName in files:
	portion = os.path.splitext(fileName)
	# 如果后缀是.dat 
	if portion[1] == ".dat":
		#把原文件后缀名改为 txt
		newName = portion[0] + ".txt" 
		os.rename(fileName, newName)

分享到 :
相关推荐

Leave a Reply

Your email address will not be published. Required fields are marked *