Python 属性错误:'_io.TextIOWrapper';对象没有属性';rpartition';

Python 属性错误:'_io.TextIOWrapper';对象没有属性';rpartition';,python,python-3.x,csv,argparse,Python,Python 3.x,Csv,Argparse,我得到这个错误: AttributeError:“\u io.TextIOWrapper”对象没有属性“rpartition” 每次我开始我的脚本 我不知道我为什么会犯这个错误。有人能帮我解决这个问题吗 我的代码: import os import json import random import csv from pydub import AudioSegment file_path = open('/path/to/file/.tsv ', encoding='UTF-8') save

我得到这个错误:

AttributeError:“\u io.TextIOWrapper”对象没有属性“rpartition”

每次我开始我的脚本

我不知道我为什么会犯这个错误。有人能帮我解决这个问题吗

我的代码:

import os
import json
import random
import csv
from pydub import AudioSegment


file_path = open('/path/to/file/.tsv ', encoding='UTF-8')
save_json_path = '/path/where/you/want/the/jsons/saved' 

def main(args):
    data = []
    directory = file_path.rpartition('/')[0]
    percent = int(100)
    
    with open(file_path) as f:
        lenght = sum(1 for ine in f)
    
    
    
    
    with open(file_path, newline='') as csvfile: 
        reader = csv.DictReader(csvfile, delimiter='\t')
        index = 1
        if(args.convert):
            print(str(lenght) + "files found")
        for row in reader:  
            file_name = row['path']
            filename = file_name.rpartition('.')[0] + ".wav"
            text = row['sentence']
            if(args.convert):
                data.append({
                "key": directory + "/clips/" + filename,
                "text": text
                })
                print("converting file " + str(index) + "/" + str(lenght) + " to wav", end="\r")
                src = directory + "/clips/" + file_name
                dst = directory + "/clips/" + filename
                sound = AudioSegment.from_mp3(src)
                sound.export(dst, format="wav")
                index = index + 1
            else:
                data.append({
                "key": directory + "/clips/" + file_name,
                "text": text
                })
                
    random.shuffle(data)

    print("creating JSON's")
    f = open(save_json_path +"/"+ "train.json", "w")
    
    with open(save_json_path +"/"+ 'train.json','w') as f:
        d = len(data)
        i=0
        while(i<int(d-d/percent)):
            r=data[i]
            line = json.dumps(r)
            f.write(line + "\n")
            i = i+1
    
    f = open(save_json_path +"/"+ "test.json", "w")

    with open(save_json_path +"/"+ 'test.json','w') as f:
        d = len(data)
        i=int(d-d/percent)
        while(i<d):
            r=data[i]
            line = json.dumps(r)
            f.write(line + "\n")
            i = i+1
    print("Done!")

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="""
    Utility script to convert commonvoice into wav and create the training and test json files for speechrecognition. """
    )  
    parser.add_argument('--convert', default=True, action='store_true',
                        help='says that the script should convert mp3 to wav')

    
    args = parser.parse_known_args()
    main(args)
导入操作系统
导入json
随机输入
导入csv
从pydub导入音频段
file_path=open('/path/to/file/.tsv',encoding='UTF-8')
save_json_path='/path/where/you/want/the/jsons/saved'
def主(args):
数据=[]
directory=file_path.r分区('/')[0]
百分比=整数(100)
打开(文件路径)为f时:
长度=总和(对于f中的线为1)
打开(文件路径,换行符=“”)作为csvfile:
reader=csv.DictReader(csvfile,分隔符='\t')
索引=1
如果(参数转换):
打印(str(长度)+“找到文件”)
对于读取器中的行:
文件名=行['path']
filename=file_name.rpartition('.')[0]+.wav
text=行[“句子”]
如果(参数转换):
data.append({
“key”:目录+“/clips/”+文件名,
“文本”:文本
})
打印(“将文件”+str(索引)+“/”+str(长度)+“转换为wav”,end=“\r”)
src=目录+“/clips/”+文件名
dst=目录+“/clips/”+文件名
声音=音频段。来自mp3(src)
声音输出(dst,format=“wav”)
索引=索引+1
其他:
data.append({
“键”:目录+“/clips/”+文件名,
“文本”:文本
})
随机。随机(数据)
打印(“创建JSON”)
f=打开(保存路径+“/”+“train.json”,“w”)
打开时(保存路径+“/”++train.json',w')为f:
d=长度(数据)
i=0
而(i在第8行:

file\u path=open('/path/to/file/.tsv',encoding='UTF-8')
打开该文件,它将为您提供一个文件句柄

第13行:

directory=file_path.r分区('/')[0]
rpartition
在文件句柄上被调用,但它是一个字符串方法

将第8行更改为:

file_path='/path/to/file/.tsv'

将清除此问题。

为什么重复的
会打开
(不带
和带
)?