2

Sorry if this question is dumb. I am using python subprocess statement to call a .bat file in Ubuntu (Natty 11.04), however, I got error messages:

Traceback (most recent call last):
  File "pfam_picloud.py", line 40, in <module>
    a=subprocess.Popen(src2, shell=0)
  File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1213, in _execute_child
    raise child_exception

run this python file

$python pfam_picloud.py

Python code (pfam_picloud.py)

#!/usr/bin/python
#
met="wTest.dvf"
run="run_pfam.bat"
inp="pfam_input.PFA"
import os
import stat
import shutil
import subprocess
import string
import random
# Generate a random ID for file save
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range(size))
name_temp=id_generator()
cwd=os.getcwd()
src=cwd
src1=cwd+'/'+name_temp
if not os.path.exists(src1):
    os.makedirs(src1)
else:
    shutil.rmtree(src1)
    os.makedirs(src1)
##
shutil.copy(src+"/"+run,src1)
shutil.copy(src+"/"+met,src1)
shutil.copy(cwd+"/pfam_pi.exe",src1)
shutil.copy(src+"/"+inp,src1)
#
src2=src1+"/run_pfam.bat"
os.chdir(src1)
a=subprocess.Popen(src2, shell=0)
a.wait()

bash file (run_pfam.bat)

#!/bin/sh
./pfam_pi.exe pfam_input.PFA

I can successfully run this bash file in Ubuntu. So I guess, I messed up something in my Python script. Could anyone give me some suggestions? Thanks for any inputs.

EDIT

the file pfam_pi.exe is a Linux executable. I compiled it in Ubuntu. Sorry for the confusion.

update

Well, I got different types of error now. 1. With #!/bin/sh, it said No such file or directory. 2. With /bin/sh, it said exec format error. 3. If I sent everything as arguments a=subprocess.Popen(['./pfam_pi.exe', 'inp', 'src1'], shell=0), it said end of line symbol error

8
  • 2
    Something tells me you're trying to execute a Windows program on Linux. What does file pfam_pi.exe say? Commented Feb 25, 2013 at 17:16
  • the pfam_pi.exe was compiled in Ubuntu. I just called it .exe sorry for the confusion Commented Feb 25, 2013 at 17:40
  • Is the script (that's not a BAT file, please don't use Windows terms when working on Linux) marked executable? Commented Feb 25, 2013 at 19:43
  • 1
    The problem should be with the parameters you actually pass to Popen. I would prefer to see a full traceback for an error #3 (end of line). The example you gave us has 'inp' and 'src1' strings an the call to Popen. Is it a typo? And if it is, and really you pass variables src and inp1, print their values. Commented Feb 26, 2013 at 16:06
  • 1
    @Ellioh: Thanks for your comments. I found once I changed the shell=1, problem is solved. Commented Feb 27, 2013 at 4:16

2 Answers 2

1

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

@Ellioh: Thanks for your comments. I found once I changed the shell=1, problem is solved. – tao.hong

Sign up to request clarification or add additional context in comments.

1 Comment

That would be useful, so that someone looking for the solution can easily identify it. Or you might post your solution as another answer, so that you can get reputation points for it; I could then (vote to) delete my copied answer.
0

Try running wine (you should have it installed) and pass pfam_pi.exe to it as a parameter. Maybe pfam_pi.exe is not a Linux executable. :-) Certainly, executable file extensions are not meaningful on Linux, but probably it really is a Windows program, otherwise I hardly can imagine it named pfam_pi.exe.

However, if it is a Linux executable, note subprocess.Popen accepts a list of args (the first element is the program itself), not a command line:

>>> import shlex, subprocess
>>> command_line = raw_input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print args
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!

5 Comments

Thanks for the reply. Actually, I compiled the pfam_pi.exe on Ubuntu. So it is a Linux executable.
Yes, I see. Probably you are trying to create a configuration that can be run with the same .bat file both on Linux and Windows...
Based on your sample code, does that mean subprocess could not run .bat file? I have tested my approach in Window, which works fine...
If it has executable flag and #!/bin/sh in the beginning, it should work. But you may also try running /bin/sh explicitly, at least to locate the problem.
Thanks for the suggestions. I tried your approach but got different error messages. Please see my updates for detail.

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.