对小白可以提供一点点点点的参考
import os
from pathlib import Path
import time
import datetime
zsj_name=input('请输入名称前1-2位:').upper()
lu1 = os.path.abspath('./')
os.makedirs(lu1+"\\zsj")
def file_name():
for root, dirs, files in os.walk(lu1):
for file in files:
if os.path.splitext(file)[1] == '.zhd':
lu = root
if lu[-2:].isdigit():
zsj_day = int(lu[-2:])
zsj_month = int(lu[-9:-7])
zsj_year = int(lu[-20:-16])
d1 = datetime.datetime.strptime(
'{year}-{month}-{day}'.format(year=zsj_year, month=zsj_month, day=zsj_day),
'%Y-%m-%d')
d2 = datetime.datetime.strptime('{year}-01-01'.format(year=zsj_year), '%Y-%m-%d')
nianjiri = '{:0>3d}'.format((d1 - d2).days + 1)
tt = os.path.splitext(file)[0][-2:]
if tt=='Z1':
tt1 = os.path.splitext(file)[0][-3:-1]
os.rename(lu + '\\' + file, lu1 + '\\zsj\\' + zsj_name + tt1 + nianjiri + '0' + '.zhd')
print(lu, file, ">>>", zsj_name + tt1 + nianjiri + '0' + '.zhd')
elif tt=='C1' or tt=='C2' or tt=='C3' or tt=='C4':
tt1 = os.path.splitext(file)[0][-3:-2]
tt2 = os.path.splitext(file)[0][-1:]
os.rename(lu + '\\' + file, lu1 + '\\zsj\\' + zsj_name + tt1+ tt2 + nianjiri + '0' + '.zhd')
print(lu, file, ">>>", zsj_name + tt1+tt2 + nianjiri + '0' + '.zhd')
else:
os.rename(lu+'\\'+file,lu1+'\\zsj\\'+zsj_name+tt+nianjiri+'0'+'.zhd')
print(lu,file,">>>",zsj_name+tt+nianjiri+'0'+'.zhd')
print('完成')
time.sleep(2)
file_name()
注意:含自带的OS模块的程序在运行时会被系统判为有风险!