这是运行在Python shell脚本的正确方法? [英] Is this the right way to run a shell script inside Python?

查看:282
本文介绍了这是运行在Python shell脚本的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导入子
RET code = subprocess.call([/家/ myuser的/ go.sh,的abc.txt,xyz.txt将该])

当我运行这两条线,我会做正是这一点:

  /home/myuser/go.sh的abc.txt xyz.txt将该

为什么我得到这个错误?但是,当我运行go.sh通常情况下,我没有得到这个错误。

 文件/usr/lib/python2.6/subprocess.py,线路480,呼叫
    返回POPEN(* popenargs,** kwargs).wait()
  文件/usr/lib/python2.6/subprocess.py,线路633,在__init__
    errread,ERRWRITE)
  文件/usr/lib/python2.6/subprocess.py,线路1139,在_execute_child
    提高child_exception
OSERROR:[错误8] Exec的格式错误


解决方案

  

OSERROR:[错误8] Exec的格式错误


这是试图运行时,操作系统报告错误 /home/myuser/go.sh

go.sh

在我看来像家当(#!)线是无效的。

下面是从外壳运行,但不会从 POPEN 示例脚本:

 #\\!/ bin / sh的
回声你刚刚叫$ 0 $ @。

删除 \\ 从第一线解决问题。

import subprocess
retcode = subprocess.call(["/home/myuser/go.sh", "abc.txt", "xyz.txt"])

When I run these 2 lines, will I be doing exactly this?:

/home/myuser/go.sh abc.txt xyz.txt

Why do I get this error? But when I run go.sh normally, I don't get that error.

File "/usr/lib/python2.6/subprocess.py", line 480, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
    raise child_exception
OSError: [Errno 8] Exec format error

解决方案

OSError: [Errno 8] Exec format error

This is an error reported by the operating system when trying to run /home/myuser/go.sh.

It looks to me like the shebang (#!) line of go.sh is not valid.

Here's a sample script that runs from the shell but not from Popen:

#\!/bin/sh
echo "You've just called $0 $@."

Removing the \ from the first line fixes the problem.

这篇关于这是运行在Python shell脚本的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆