Python sys.argv 和定义无效的输入组合
Python sys.argv and input combination with a definition NOT working
我有一个像下面这样的 python 脚本,我试图在 shell 中 运行。我想使用 argv
或 input
来提供名为 index1 和 index2(ACCGTCG
和 TTCCAGC
)的特定输入以及要处理的文件名。我尝试了如下两种方式(使用 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__':
dual_index_positions()
使用这个
if __name__ == '__main__':
dual_index_positions()
__name__ == '__dual_index_positions__'
的 RHS 不是您必须调用的函数名称,而是模块的名称,默认情况下它的值为 __main__
我有一个像下面这样的 python 脚本,我试图在 shell 中 运行。我想使用 argv
或 input
来提供名为 index1 和 index2(ACCGTCG
和 TTCCAGC
)的特定输入以及要处理的文件名。我尝试了如下两种方式(使用 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__':
dual_index_positions()
使用这个
if __name__ == '__main__':
dual_index_positions()
__name__ == '__dual_index_positions__'
的 RHS 不是您必须调用的函数名称,而是模块的名称,默认情况下它的值为 __main__