gpt4 book ai didi

python - os.system 提示圆括号

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:22 27 4
gpt4 key购买 nike

我正在尝试将这一行包含在 python 脚本中。

!#/bin/bash/env python

import os

os.system("paste <(awk '!/^($|[:space:]*#)/{print $0}' file1) <(awk '!/^($|[:space:]*#)/{print $0} file2) > out_file")

直接从 bash 运行该命令非常好。但是,在脚本中,我得到:

sh: -c: line0: syntax error near unexpected token `('

简单使用时问题依旧:

os.system("paste <(cat file1) > output_file")

有什么想法吗?

最佳答案

The command is perfectly fine when run from bash directly. However, inside the script, I get:

sh: -c: line0: syntax error near unexpected token `('

那是因为在脚本中,您正在使用 sh 而不是 bash 运行命令。此命令和更简单的命令都使用 bash 特定的功能。尝试运行 sh shell 并键入相同的行,您将得到相同的错误。

os.system call 没有记录它使用的是什么 shell,因为它是:

implemented by calling the Standard C function system()

在大多数类 Unix 系统上,这调用 sh。您可能不应该依赖它……但您绝对不应该依赖它调用 bash!

如果你想运行 bash 命令,使用 subprocess模块,并显式运行 bash:

subprocess.call(['bash', '-c', 'paste <(cat file1) > output_file'])

我想,您可以尝试获得引用权以运行 bash 作为 system 使用的 shell 中的子 shell……但何必呢?

这是文档反复告诉您应该考虑使用 subprocess 而不是 os.system 的众多原因之一。

关于python - os.system 提示圆括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com