调用外部可执行程序时用到了os.system(),之后读取文件时报错FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\3101000001-3101000411\\sp_31000001710.f0'
tmp_f0 = os.path.join(wavPath, 'sp_' + str(name) + '.f0')
os.system('{0} -C {1} -r 0.005 -n 40 -x 1000 -g 1 {2} {3}'.format(f0Path, f0confpath, tmp_wav_debit, tmp_f0))
nonsilList = []
with open(tmp_f0, 'r') as f0F:
scr = f0F.read().split()
for i in scr:
if float(i) > 0:
nonsilList.append(float(i))
原因是需要生成f0的文件比较大os.system()执行还没有成功,下面就已经开始读取文件导致FileNotFoundError,这种情况下需要在读取文件之前sleep几秒,可按文件大小设置需要sleep多少秒
tmp_f0 = os.path.join(wavPath, 'sp_' + str(name) + '.f0')
os.system('{0} -C {1} -r 0.005 -n 40 -x 1000 -g 1 {2} {3}'.format(f0Path, f0confpath, tmp_wav_debit, tmp_f0))
if not os.path.exists(tmp_f0):
sleep(5)
nonsilList = []
with open(tmp_f0, 'r') as f0F:
scr = f0F.read().split()
for i in scr:
if float(i) > 0:
nonsilList.append(float(i))
成功,Thanks!