简体   繁体   English

Python子流程Exec格式错误

[英]Python subprocess Exec format error

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: 我正在使用python subprocess语句在Ubuntu(Natty 11.04)中调用.bat文件,但是,我收到了错误消息:

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文件

$python pfam_picloud.py

Python code (pfam_picloud.py) Python代码(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) bash文件(run_pfam.bat)

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

I can successfully run this bash file in Ubuntu. 我可以在Ubuntu中成功运行此bash文件。 So I guess, I messed up something in my Python script. 所以我想我在Python脚本中弄乱了一些东西。 Could anyone give me some suggestions? 有人可以给我一些建议吗? Thanks for any inputs. 感谢您的任何投入。

EDIT 编辑

the file pfam_pi.exe is a Linux executable. pfam_pi.exe文件是Linux可执行文件。 I compiled it in Ubuntu. 我在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 . 1.使用#!/bin/sh表示No such file or directory 2. With /bin/sh , it said exec format error . 2.用/bin/sh表示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 3.如果我将所有内容都作为参数a=subprocess.Popen(['./pfam_pi.exe', 'inp', 'src1'], shell=0) ,它表示end of line symbol error

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here. 由于功能请求将评论标记为答案的请求仍然被拒绝,因此我将上述解决方案复制到此处。

@Ellioh: Thanks for your comments. @Ellioh:感谢您的评论。 I found once I changed the shell=1, problem is solved. 我发现一旦更改shell = 1,问题就解决了。 – tao.hong – tao.hong

Try running wine (you should have it installed) and pass pfam_pi.exe to it as a parameter. 尝试运行wine (您应该已经安装了wine ),并将pfam_pi.exe作为参数传递给它。 Maybe pfam_pi.exe is not a Linux executable. 也许pfam_pi.exe不是Linux可执行文件。 :-) 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 . :-)当然,可执行文件扩展名在Linux上没有意义,但可能确实是Windows程序,否则我很难想象它名为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: 但是,如果它是Linux可执行文件,请注意subprocess.Popen接受args列表(第一个元素是程序本身),而不是命令行:

>>> 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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM