微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python3 - 如何生成名称是输入文件名称修改的输出文件?

如何解决python3 - 如何生成名称是输入文件名称修改的输出文件?

您好,我正在使用包含以下代码的 Python 脚本:

 public partial class Module_Completion
{
    public int module_completionId { get; set; }

    [StringLength(10)]
    public string star_id { get; set; }

    public bool adv_stud_success { get; set; }

    public bool important_policies { get; set; }

    public bool technology { get; set; }

    public bool finances { get; set; }

    public bool resources { get; set; }

    public bool student_life { get; set; }

    public bool next_steps { get; set; }
}

问题是我想将此脚本自动化到某个目录中的多个文件,这样我希望 from Bio import SeqIO # set input file,output file to write to gbk_file = "bin.10.gbk" tsv_file = "results.bin_10.tsv" cluster_out = open(tsv_file,"w") # Extract CLuster info. write to file for seq_record in SeqIO.parse(gbk_file,"genbank"): for seq_feat in seq_record.features: if seq_feat.type == "protocluster": cluster_number = seq_feat.qualifiers["protocluster_number"][0].replace(" ","_").replace(":","") cluster_type = seq_feat.qualifiers["product"][0] cluster_out.write("#"+cluster_number+"\tCluster Type:"+cluster_type+"\n") 存储所有以 .gbk 作为后缀的文件,并且 gbk_file根据每个输入文件生成相应的输出文件

因此,如果输入文件名称为“bin.10.gbk”,则输出将是“results.bin_10.tsv”。

我尝试使用 glob python 函数,但不知道如何创建一个 tsv_file 变量来存储来自输入文件名的修改后的字符串:

tsv_file

进行更改后,出现以下错误

AttributeError: 'list' 对象没有属性 'replace'

那我该如何处理?

感谢阅读:)

解决方法

希望以下功能可以帮到您。

def processfiles():
    for file in glob.glob("*.gbk"):
        names = file.split('.')
        tsv_file = f'results.{names[-3]}_{names[-2]}.tsv'
        with open(tsv_file,'w') as tsv:
            tsv.write('write your content here')
            tsv.close

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。