Python 批量处理文件

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

Python 批量处理文件_第1张图片

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

Python 批量处理文件_第2张图片


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

Python 批量处理文件_第3张图片

    所以选择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文档,如下:

Python 批量处理文件_第4张图片

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

Python 批量处理文件_第5张图片

 可作如下处理:

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


你可能感兴趣的:(python笔记)