6_文件IO
1. 基本文件读取
readline(),readlines(),write(),writelines()
f.read(size),指定读取文件的字节数,需要注意的是,对于同一个文件,一直调用这个函数,文件指针会一直移动直到文件尾。
所以如果要中途修改文件指针的位置,那么需要调用seek()函数。常用的是seek(0):移动到文件头;seek(n):移动到文件位置n处;seek(0,n):移动到文件尾。
f.tell()函数存储文件指针当前的位置
2. 文件操作
2.1 文件复制
除了手动编写,也可以使用现成模块
import shutil
shutil.copyfile('1.txt','2.txt')
##复制文件2.txt到1.txt中
2.2 文件删除
import os,os.path
filename = 'test.txt'
if os.path.exists(filename): ##判断文件是否存在
os.remove(filename)
else:
print('not exist!')
2.3 文件重命名
import os
filename = 'test.txt'
rename = 'write.txt'
os.rename(filename,rename)
3. 目录操作
3.1 目录创建
import os
os.listdir('f:/')
os.mkdir('f:/newdir') ##创建一个新目录
os.mkdirs('f:/newdir1/newdir2') ##创建一个二级目录
3.2 目录删除
import os
os.rmdir('f:/newdir') ##只能删除空目录
os.removedirs('f:/newdir1/newdir2') ##删除两级目录
shutil.rmtree('f:/newdir') ##可以删除非空目录
3.3 目录遍历
###递归法
import os
def visitdir1(path):
for lists in os.listdir(path):
sub_path = os.path.join(path,lists)
print(sub_path)
if os.path.isdir(sub_path):
visitdir(sub_path)
`````````````````````````````````````````````````````````````````````````````````````
###os.walk法
import os
def visitdir2(path):
list_dirs = os.walk(path) ##返回三元组:路径名,目录列表,文件列表
for root,dirs,files in list_dirs:
for d in dirs:
print(os.path.join(root,d))
for f in files:
print(os.path.join(root,f))
4. 综合应用
题目
音频文件的根目录在:/dnn4_added/0_ASR/01_resource/original_speech_data/AISHELL-ASR009-zh-cn-mandarin178/data_aishell/wav
说话信息在/dnn4_added/0_ASR/01_resource/original_speech_data/AISHELL-ASR009-zh-cn-mandarin178/data_aishell/transcript/aishell_transcript_v0.8.txt
请得到一个列表文件,文件里面显示音频全路径和说话信息,并把说话信息里的空格都去掉。并且把音频文件的后缀名改成.mfcc
代码
import os
import os.path
import json
import sys
filter = [".wav"] ##设置文件过滤后缀
postfix = '.mfcc' ##题目要求的后缀
dirrname = '/dnn4_added/0_ASR/01_resource/original_speech_data/AISHELL-ASR009-zh-cn-mandarin178/data_aishell/wav'
filename = '/dnn4_added/0_ASR/01_resource/original_speech_data/AISHELL-ASR009-zh-cn-mandarin178/data_aishell/transcript/aishell_transcript_v0.8.txt'
##目录遍历,把wav文件都找出来
def all_path(dirname):
PATHS = []
for maindir, subdir, file_name_list in os.walk(dirname):
for filename in file_name_list:
apath = os.path.join(maindir,filename)
ext = os.path.splitext(apath)[1]
if ext in filter:
PATHS.append(apath)
return PATHS
PATHS = all_path(dirrname)
##read txt as dictionary
dict = {}
with open(filename,'r') as file_object:
for line in file_object:
split_list = line.split(' ',1)
split_list[1] = ''.join(split_list[1].split())
dict[split_list[0]] = split_list[1]
##find key in dictionary and update it
for path in PATHS:
split_dirname = os.path.splitext(path)[0]
split_basename = os.path.basename(split_dirname)
if dict.has_key(split_basename):
split_dirname += postfix
dict[split_dirname] = dict.pop(split_basename)
##write the new dictionary as txt
keys = list(dict.keys())
values = list(dict.values())
z = list(zip(keys,values))
outputfile = open('dic.txt','w')
for row in z:
rowtext = '{} {}'.format(row[0],row[1])
outputfile.write(rowtext)
outputfile.write('\n')
outputfile.close