重写一个最简版(以下代码存为test.py,与被处理文件同目录),主要解决楼主的核心需求
运行test.py的结果》当前目录中(不包括子目录)的全部*_01.txt被逐一生成对应的倒序文件DaoXun_*_nn.txt,倒序文件与源文件同目录- import os,sys
- def rF (F, sq, qn):
- with open(F, 'r') as fr: lines = fr.readlines()
- nF = newF(F, sq, qn)
- with open(nF, 'w') as fw:
- for line in reversed(lines): fw.write(line)
- def newF (F, sq, qn):
- global q
- np = F.rfind('\\'); mp = F.rfind('_'); xp = F.rfind('.'); q += int(F[mp+1:xp]); qs = f'{sq}{str(q)}'[-qn:]
- return F[:np+1]+'DaoXu_'+F[np+1:mp+1]+qs+F[xp:]
- ####
- q = 0
- files = os.popen('dir /b/a-d *_01.txt 2>nul').read().splitlines()
- if not files: sys.exit()
- qn = len(str(len(files))); sq = '0'*len(str(len(files)))
- for f in files: rF(f, sq, qn)
- sys.exit()
复制代码