Python 批量處理檔案

hearthougan發表於2017-07-13

    把一個檔案下有許多資料夾,並且其中每個檔案中又有很多檔案(每個資料夾下的檔案數量並不一定相同)(如下圖)。

    如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)


相關文章