【问题标题】:Python sys.argv and input combination with a definition NOT workingPython sys.argv 和带有定义的输入组合不起作用
【发布时间】:2020-11-10 08:15:21
【问题描述】:

我有一个如下所示的 python 脚本,我试图在 shell 中运行。我想使用argvinput 来提供名为index1 和index2(ACCGTCGTTCCAGC)的特定输入以及要处理的文件名。我尝试了两种方法(分别使用 sys.argv 和输入),但我没有得到任何输出。

使用 sys.argv:

#!/usr/bin/python

import sys
from Bio import SeqIO

def dual_index_positions():
    script=sys.argv[0]
    index1=sys.argv[1]
    index2=sys.argv[2]
    input_file=sys.argv[3]
    count=0 
    with open(input_file, "r") as Fastq:
        for record in SeqIO.parse(Fastq,'fastq'): 
            if index1 in record.seq and index2 in record.seq: 
                print(record.name)
                ind1_rec=record.seq.find(index1) 
                ind2_rec=record.seq.find(index2)
                rp_ind2=ind2_rec+len(index2)
                dist=(ind1_rec)-(rp_ind2)
                print('Index1 and index2 positions are '+ str(ind1_rec+1) + ' and ' + str(ind2_rec+1) + ' respectively' +
                  '; distance is: ' + str(dist))
                count+=1
    print('The total number of hits is: '+ str(count))
    


if __name__ == '__dual_index_positions__':
    dual_index_positions()

接受输入:

#!/usr/bin/python

from Bio import SeqIO 

def dual_index_positions(): 
    input_file=input('please enter your input_file: ')
    index1=str(input('please enter your index 1: '))
    index2=str(input('please enter your index 2: '))
    count=0 
    with open(input_file, "r") as Fastq:
        for record in SeqIO.parse(Fastq,'fastq'):
            if index1 in record.seq and index2 in record.seq: 
                print(record.name)
                ind1_rec=record.seq.find(index1) 
                ind2_rec=record.seq.find(index2)
                rp_ind2=ind2_rec+len(index2)
                dist=(ind1_rec)-(rp_ind2)
                print('Index1 and index2 positions are '+ str(ind1_rec+1) + ' and ' + str(ind2_rec+1) + ' respectively' +
                  '; distance is: ' + str(dist))
                count+=1
    print('The total number of hits is: '+ str(count))
    


if __name__ == '__dual_index_positions__':
    dual_index_positions()

有人可以帮我找出错误在哪里吗?提前谢谢你。

【问题讨论】:

  • “我没有输出”是什么意思?
  • if __name__ == '__dual_index_positions__': 这通常是if __name__ == '__main__': 如果您直接运行它,则不要使用那里的文件名
  • 我使用 shell 运行代码,但没有处理或打印任何内容。

标签: python input pycharm definition argv


【解决方案1】:

不是这个

if __name__ == '__dual_index_positions__':
    dual_index_positions()

使用这个

if __name__ == '__main__':
    dual_index_positions()

__name__ == '__dual_index_positions__' 的 RHS 不是您必须调用的函数名,而是模块的名称,默认值为 __main__

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多