美文网首页
黑猴子的家:Pyhton 批量修改文件名

黑猴子的家:Pyhton 批量修改文件名

作者: 黑猴子的家 | 来源:发表于2022-06-08 11:15 被阅读0次

1、a.txt

261fb059-8893-4544-b457-dca0bf452866.MP4
ee762e53-7578-44c3-b119-da13f7c35248.MP4
0d097274-ec7c-4f08-9455-cb13a17cba6a.MP4
4f19a1eb-f89f-477a-8089-393289a41c40.MP4
f8c5b8a6-677b-4f6a-bfc9-bfc7a6a37a25.MP4
202e51d0-a84e-4fce-83f0-392208ee071d.MP4

2、b.txt

01_Linux命令1.MP4
02_Linux命令2.MP4
03_Linux命令3.MP4
04_Linux命令4.MP4
05_Linux命令5.MP4
06_Linux命令6.MP4

3、simulation.py模拟生成文件

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os
af_name = 'a.txt'
af = open(af_name, 'r', encoding='utf-8')

def mk_file_io(fname):
    fio = open('view'+ os.sep + fname.strip('\n').strip('\r'), 'w', encoding='utf-8')
    return fio

for fname in af:
    f_new = mk_file_io(fname)
    f_new.write('123'.strip()+'\n')
    f_new.write('456'.strip()+'\n')
    f_new.close()

4、mv-a-to-b.py 批量修改文件

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os

path = r'view'

af_name = 'a.txt'
afile = open(af_name, 'r', encoding='utf-8')

bf_name = 'b.txt'
bfile = open(bf_name, 'r', encoding='utf-8')

a_list = afile.readlines()
b_list = bfile.readlines()

afile.close()
bfile.close()

for i in range(len(a_list)):
    oldname = path + os.sep + a_list[i].strip('\n').strip('\r')
    newname = path + os.sep + b_list[i].strip('\n').strip('\r')
    os.rename(oldname, newname)
    #os.system(f'mv {oldname} {newname}')
    print(f'{oldname} -> {newname}')

相关文章

网友评论

      本文标题:黑猴子的家:Pyhton 批量修改文件名

      本文链接:https://www.haomeiwen.com/subject/ypkumrtx.html