# 批量修改文件名
import os
import xlwt
import xlrd
import sys
text = "F:\\成绩表"
dirs = os.listdir(text)
for dir in dirs:
currentpath1 = text
xh_xm = '_'.join(dir.split('-')); # 改成格式为:学号_姓名
directory = text + '\\' + dir # 赋值当前工作目录
# print(os.listdir(directory))
files = os.listdir(directory)
# 遍历文件夹内的所有文件
for file in files:
path = directory # 赋值当前工作目录
if ".docx" in file: # 指定文件类型
# print(file)
os.chdir(path)
file_newname = os.rename(os.path.join(path,file),os.path.join(path,(xh_xm + '.docx'))) #文件重新命名
elif ".doc" in file: # 指定文件类型
# print(file)
os.chdir(path)
file_newname = os.rename(os.path.join(path,file),os.path.join(path,(xh_xm + '.doc'))) #文件重新命名
else:
os.chdir(currentpath1) # 改回程序运行前的工作目录
sys.stdin.flush() #刷新