【问题标题】:Python subprocess gives OSError: [WinError 193] %1 is not a valid Win32 applicationPython 子进程给出 OSError: [WinError 193] %1 is not an valid Win32 application
【发布时间】:2019-02-24 07:29:22
【问题描述】:

我正在编写一个需要调用 bash 脚本的 python 脚本。 python 脚本包含变量a1a2,需要作为参数传递给bash 脚本。 我已经导入了 subprocess 并像这样运行它:

subprocess.check_call( ["./test.sh", a1, a2], shell=False)

但我从这一行得到一个错误,

OSError: [WinError 193] %1 不是有效的 Win32 应用程序。 我应该做些什么不同的事情?

【问题讨论】:

  • 你正在尝试在 windows 中调用 unix 脚本?
  • UNIX 支持#! 行,Windows 不支持。您的 test.sh 将没有 Windows 可执行文件的正确幻数或文件关联。

标签: python bash operating-system subprocess


【解决方案1】:

如果你有“Windows 上的 Ubuntu 上的 Bash”,你可以这样调用你的脚本:

subprocess.check_call( ['bash', '-c', './test.sh', a1, a2], shell=False)

要在 Windows 上使用此功能,您需要 Windows 10,您可以按照this link 中的说明进行操作。

一般来说,即使在 unix 机器上,这也是一种更好的方法(尽管在大多数机器中 .sh 文件只会执行而不在 bash 中显式运行它们)

【讨论】:

    猜你喜欢
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多