这个错误使我发疯。我的脚本输出错误:
>>>Run shell cmd "grep -vw ^# *.blastn | awk $1 != $2 > *matchfile*"
grep: /projects/percid100_2/blastn.outfile: No such file or directory
2
我检查了文件,它肯定在那里。
ll /projects/percid100_2/blastn.outfile
-rw-r--r-- 1 users 42633 Apr 17 12:34 /projects/percid100_2/blastn.outfile
上一个功能:
def run_blastn(outdir, outfile):
"""Run blastn under given percent identity """
print ">>> Run blastn"
blastnlog = os.path.join(outdir, 'blastn_db_log')
# make database and run blastn
ref = Popen(['cmd1', '-logfile', blastnlog])
ref.communicate()
blastn = Popen(['cmd2', '-out', outfile], stderr=PIPE)
发生功能错误:
def filter_query(infile, matchfile):
"""Filter out self to self hit and no hit"""
print ">>> Filter query self to self hit and no hit"
print('>>> Run shell cmd "grep -vw ^# *.blastn | awk $1 != $2 > *matchfile*"')
grep = Popen(['grep', '-vw', '^#', infile], stdout=PIPE)
awk = Popen(['awk', '$1 != $2'], stdin=grep.stdout, stdout=PIPE)
output = awk.communicate()[0]
grep.communicate()
if grep.returncode != 0:
print grep.returncode
sys.exit()
with open(matchfile, 'wb') as ofile:
print 'Write to file %s' % matchfile
ofile.write(output)
主功能:
def main():
parser = get_parser()
args = parser.parse_args()
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)
outdir = os.path.abspath(args.outdir)
bloutfile = 'blastn.outfile'
path_bloutfile = os.path.join(outdir, bloutfile)
# filter query seq outfile name
matchfile = 'match_file'
path_matchfile = os.path.join(outdir, matchfile)
# run blastn
run_blastn(outdir, path_bloutfile)
# filter blastn output gain only matching information
filter_query(path_bloutfile, path_matchfile)
if __name__=='__main__':
main()
函数输入之一infile
是通过subprocess.Popen
调用另一个程序从先前的函数生成的。
我对这个问题的猜测是先前的命令已完成,并且此子进程调用无法识别先前函数的输出文件。我不知道我应该寻找什么解决方案。
如果我尝试多次运行脚本,该脚本最终将成功运行。
但是,这不行。
我尝试使用os.path.abspath()
,但无法解决此问题。
我愿意打赌,问题出在您描述的代码中,但是没有向我们展示,该代码运行“先前命令”,该命令生成grep
正在寻找的文件。
如果您通过创建来运行前一个命令Popen
,但未wait
在其上运行,则该命令仍将在后台运行。如果启动grep
太快,则可能尚未创建该文件。所以你得到了错误。
然后,它需要几秒钟,你去看看在外壳的文件,到那个时候,它已经被创建。因此错误看起来令人困惑。
或者,如果您多次运行该程序,则最终可以正常工作-是因为您对计时感到幸运,或者是因为新运行找到了前一次运行遗留的文件。
该修复程序可能只是添加了missing other_command.communicate()
,但没有看到其他代码,很难确定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句