sh: 1: Syntax error: redirection unexpected Python/Bash
sh: 1: Syntax error: redirection unexpected Python/Bash
我的 python cmd 脚本有问题。
我不知道为什么它不起作用。也许我的代码有问题。
我正在尝试通过我的 python 脚本 运行 cmdline 中的程序。
我在 bash "sh: 1: Syntax error: redirection unexpected"
中遇到错误
请帮助我只是生物学家:)
我正在使用 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("------------------------------------------")
output_file
是一个文件对象。当您执行 "%s" % output_file
时,生成的字符串类似于 "<open file 'filename', mode 'a+' at 0x7f1234567890>"
。这意味着 os.system 调用是 运行 类似
的命令
command... >> <open file 'filename', mode 'a+' at 0x7f1234567890>
>>
之后的 <
导致 "Syntax error: redirection unexpected" 错误消息。
要解决这个问题,请不要在 Python 脚本中打开输出文件,只需使用文件名:
output_file = sys.argv[2]
我在下一行遇到了类似的错误:
os.system('logger Status changed on %s' %s repr(datetime.now())
确实,正如 nomadictype 所述,问题出在 运行 普通 OS 命令中。该命令可能包含特殊字符。就我而言,这是 <
.
因此,我没有显着更改 OS 命令,而是添加了引号,这有效:
os.system('logger "Status changed on %s"' %s repr(datetime.now())
引号使传递参数的内容对 shell 不可见。
我的 python cmd 脚本有问题。 我不知道为什么它不起作用。也许我的代码有问题。 我正在尝试通过我的 python 脚本 运行 cmdline 中的程序。
我在 bash "sh: 1: Syntax error: redirection unexpected"
中遇到错误请帮助我只是生物学家:)
我正在使用 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("------------------------------------------")
output_file
是一个文件对象。当您执行 "%s" % output_file
时,生成的字符串类似于 "<open file 'filename', mode 'a+' at 0x7f1234567890>"
。这意味着 os.system 调用是 运行 类似
command... >> <open file 'filename', mode 'a+' at 0x7f1234567890>
>>
之后的 <
导致 "Syntax error: redirection unexpected" 错误消息。
要解决这个问题,请不要在 Python 脚本中打开输出文件,只需使用文件名:
output_file = sys.argv[2]
我在下一行遇到了类似的错误:
os.system('logger Status changed on %s' %s repr(datetime.now())
确实,正如 nomadictype 所述,问题出在 运行 普通 OS 命令中。该命令可能包含特殊字符。就我而言,这是 <
.
因此,我没有显着更改 OS 命令,而是添加了引号,这有效:
os.system('logger "Status changed on %s"' %s repr(datetime.now())
引号使传递参数的内容对 shell 不可见。