sh:1:语法错误:重定向意外的Python/Bash

MTG*_*MTG 0 python bash shell bioinformatics

我的python cmd脚本有问题.我不知道为什么它不起作用.我的代码可能有问题.我试图通过我的python脚本在cmdline中运行该程序.

我在bash中得到错误"sh:1:语法错误:重定向意外"

请帮助我只是生物学家:)

我使用spyder(anaconda)/ Ubuntu

#!/usr/bin/python

import sys
import os

input_ = sys.argv[1]
output_file = open(sys.argv[2],'a+')    
names = input_.rsplit('.')

for name in names:
    os.system("esearch -db pubmed -query %s  | efetch -format xml | xtract -pattern PubmedArticle  -element AbstractText >> %s" % (name, output_file))
    print("------------------------------------------")
Run Code Online (Sandbox Code Playgroud)

nom*_*ype 5

output_file是一个文件对象.当你这样做时"%s" % output_file,结果字符串是这样的"<open file 'filename', mode 'a+' at 0x7f1234567890>".这意味着os.system调用正在运行一个命令

command... >> <open file 'filename', mode 'a+' at 0x7f1234567890>
Run Code Online (Sandbox Code Playgroud)

<>>导致"语法错误:重定向意外的"错误消息.

要解决此问题,请不要在Python脚本中打开输出文件,只需使用文件名:

output_file = sys.argv[2]
Run Code Online (Sandbox Code Playgroud)