Python 批量处理文件
来源:互联网 发布:网络攻防比赛 编辑:程序博客网 时间:2024/06/07 02:49
把一个文件下有许多文件夹,并且其中每个文件中又有很多文件(每个文件夹下的文件数量并不一定相同)(如下图)。
如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 EditorThis is a temporary script file."""import osimport shutilimport numpy as npimport 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)
阅读全文
0 0
- Python 批量处理文件
- Python 文件重命名批量处理
- python批量图片处理
- python批量处理dat文件及科学计算
- 批量处理文件
- 批量处理隐藏文件
- Matlab批量处理文件
- perl批量处理文件
- 批量文件删除处理
- python批量重命名文件
- 批量重命名文件 Python
- python批量重命名文件
- python批量删除文件
- python批量删除文件
- Python 批量修改文件
- Python 批量下载文件
- python批量修改文件
- python 批量下载文件
- tcpdump
- JSP内置对象:设置refresh属性
- Docker-利用dockerfile来搭建tomcat服务
- hihocoder 1509 异或排序 二进制思维
- spring cloud 教程
- Python 批量处理文件
- 使用自签名SSL证书配置HTTPS,解决浏览器提示不安全警告
- 013 Python语法之文件与生成器
- 统计bit 1的个数
- 在QT中,当BOM头不存在的时候,判断一个文件是否为UTF-8编码格式
- 链表排序_2.0版
- 283. Move Zeroes(C++/Java)
- loadrunner11报错:Error -27780
- HDU 2063 Investment (完全背包+数据压缩)