python中os.system()遇到的坑

本文讲述了在调用外部可执行程序os.system后,由于文件生成需要时间,可能导致读取文件时出现FileNotFoundError。通过在读取前添加sleep操作,确保文件存在,解决了这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用外部可执行程序时用到了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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值