Abner

博观而约取,厚积而薄发,不可择焉不精,语焉不详!

Python 批量处理文件

    把一个文件下有许多文件夹,并且其中每个文件中又有很多文件(每个文件夹下的文件数量并不一定相同)(如下图)。

    如1589文件夹下有三个文件(如下),但是1592文件夹下有4个文件:


    现在我想把这些文件夹下的所有音频文件,转移到一个文件夹下,并修改文件名字,如下所示:


    所以选择Python处理比较方便:

def eachFile(filepath):
    pathDir =  os.listdir(filepath)
    dic = {}
    for file_name in pathDir:
        child = os.path.join('%s/%s' % (filepath, file_name))
        doc_path = os.listdir(child)
        i = 0;
        for each_document in doc_path:
            i += 1
            tmp = os.path.join('%s/%s' % (child, each_document))
            shutil.copy(tmp, "/home/abner/Documents/world_wav")#copy tmp file to "/home/abner/Documents/world_wav"
            #rename file name
            os.rename("/home/abner/Documents/world_wav"+"/"+each_document, "/home/abner/Documents/world_wav"+"/"+file_name+"_"+str(i)+".mp3")
        dic[file_name] = i
  

    2、对于一个text文档,如下:


    我现在想修改每一行的标号,并修改成如下样式:


 可作如下处理:

    ########################################################
    #           modify the text each row                   #
    ########################################################
    utts_path = "/home/abner/Documents/world_wav/utts.data"
    write_modify_file = open("/home/abner/Documents/world_wav/Modify.data", 'w')
    with open(utts_path, 'r') as f:
        for line in f:
            stuff = re.findall('[0-9]+', line)
            get_file_name = stuff[0]
            number = int(dic[get_file_name])
            length = len(get_file_name)
            for i in range(number):
                line1 = line[0:2]+get_file_name+"_"+str(i+1)+line[2+length:]
                write_modify_file.flush()
                write_modify_file.write(line1)
    write_modify_file.close()

    all code:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import os
import shutil
import numpy as np
import re

###############################################################
#      copy documents in different files to a given file      #
###############################################################

def eachFile(filepath):
    pathDir =  os.listdir(filepath)
    dic = {}
    for file_name in pathDir:
        child = os.path.join('%s/%s' % (filepath, file_name))
        doc_path = os.listdir(child)
        i = 0;
        for each_document in doc_path:
            i += 1
            tmp = os.path.join('%s/%s' % (child, each_document))
            shutil.copy(tmp, "/home/abner/Documents/world_wav")#copy tmp file to "/home/abner/Documents/world_wav"
            #rename file name
            os.rename("/home/abner/Documents/world_wav"+"/"+each_document, "/home/abner/Documents/world_wav"+"/"+file_name+"_"+str(i)+".mp3")
        dic[file_name] = i
        
    ########################################################
    #           modify the text each row                   #
    ########################################################
    utts_path = "/home/abner/Documents/world_wav/utts.data"
    write_modify_file = open("/home/abner/Documents/world_wav/Modify.data", 'w')
    with open(utts_path, 'r') as f:
        for line in f:
            stuff = re.findall('[0-9]+', line)
            get_file_name = stuff[0]
            number = int(dic[get_file_name])
            length = len(get_file_name)
            for i in range(number):
                line1 = line[0:2]+get_file_name+"_"+str(i+1)+line[2+length:]
                write_modify_file.flush()
                write_modify_file.write(line1)
    write_modify_file.close()
     
if __name__ == '__main__':
    filePath = "/home/abner/Documents/USA/word"
    eachFile(filePath)


阅读更多
版权声明:本文为博主原创文章,转载需注明出处。 https://blog.csdn.net/Hearthougan/article/details/74991692
上一篇ubuntu 命令安装 beyond compare
下一篇浅谈全概率公式和贝叶斯公式
想对作者说点什么? 我来说一句

Python批量处理程序

2014年05月23日 3KB 下载

没有更多推荐了,返回首页

关闭
关闭