python - 这是在 Python 中运行 shell 脚本的正确方法吗?

标签 python linux bash shell unix

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

当我运行这两行时,我会这样做吗?:

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

为什么会出现此错误?但是当我正常运行 go.sh 时,我没有收到该错误。

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

这是操作系统在尝试运行 /home/myuser/go.sh 时报告的错误。

在我看来,go.sh 的 shebang (#!) 行无效。

这是一个从 shell 而非 Popen 运行的示例脚本:

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

从第一行删除 \ 可以解决问题。

关于python - 这是在 Python 中运行 shell 脚本的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834483/

相关文章:

python - 如何计算滚动窗口中数据框的列中相同实例的数量

python - 使用beautifulsoup时出现属性错误如何解决?

python - 如何在 python 3 中精确复制 python 2 chr

c - 使用 linux 功能是否禁用 LD_PRELOAD

bash - 如果 #channels 为 2,则连接 wav 文件将忽略

linux - 将字符串存储在变量中时 tr(翻译字符)出错

bash - bash 中的动态 case 语句

python - 导入同名的 Python 模块

PHP 创建的文件 | SSH 无法删除(权限被拒绝)

android - Android 上的 pcsc-lite 和 ccid