1

I have problem with my python cmd script. I don't know why it does not work. Maybe something wrong with my code. Im trying to run the program in cmdline through my python script.

And Im getting error in bash "sh: 1: Syntax error: redirection unexpected"

pls help Im just biologist :)

Im using 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("------------------------------------------")

2 Answers 2

6

output_file is a file object. When you do "%s" % output_file, the resulting string is something like "<open file 'filename', mode 'a+' at 0x7f1234567890>". This means that the os.system call is running a command like

command... >> <open file 'filename', mode 'a+' at 0x7f1234567890>

The < after the >> causes the "Syntax error: redirection unexpected" error message.

To fix that, don't open the output file in your Python script, just use the filename:

output_file = sys.argv[2]
Sign up to request clarification or add additional context in comments.

Comments

1

I got similar error on following line:

os.system('logger Status changed on %s' %s repr(datetime.now())

Indeed, as nomadictype stated the problem is in running plain OS command. The command may include special characters. In my case this was <.

So instead of changing OS command significantly, I just added quotes and this works:

os.system('logger "Status changed on %s"' %s repr(datetime.now())

Quotes make content of passed parameter invisible for shell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.