【问题标题】:How to user input filename with a specific extension?如何用户输入具有特定扩展名的文件名?
【发布时间】:2018-12-02 01:37:14
【问题描述】:

我正在尝试使用 python 创建一个程序,该程序将用户输入一个 fasta 文件,该文件以后可用于修剪引物。我正在尝试使用 BioPython 来执行此操作,但我一直遇到错误。我试过的代码如下:

from Bio import SeqIO

in_file = input("Enter filename with extension:")

def is_fasta(in_file):
    with open(in_file) as handle:
        fasta = SeqIO.parse(handle, "fasta")
        return any(fasta)

is_fasta(in_file)

我希望能够请求一个 fasta 文件,如果输入的文件不是 fasta,则显示错误消息并提示重试。

【问题讨论】:

    标签: python bioinformatics biopython


    【解决方案1】:

    你熟悉 Python 的try 语句吗?您可以尝试打开并读取文件,如果它不起作用(即引发异常),您会再次提示用户。

    我不知道Bio.SeqIO 甚至Bio 模块。我猜错误来自包含您对SeqIO.parse 的调用的行?你可以试试这样:

    from Bio import SeqIO
    
    while True:
        in_file = input("Enter filename with extension:")
        try:
            with open(in_file) as handle:
                fasta = SeqIO.parse(handle, "fasta")
        except:
            print("Could not read file. Please ensure it is a 'fasta' file and try again.")
            continue
        break
    

    无一例外,fasta 的分配应该有效,我们将用break 打破while 循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 2011-02-14
      • 2016-01-06
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多