运行 linux 命令在 Python 使用子进程时出错

Error while running linux command in Python using subprocess

此处的目标是从网站获取密钥或 p/w。

在 Python 中,我在 linux 命令后尝试了 os.system() 和 subprocess.call([]) 到 运行,但两种方法都结束了出现错误。

os.system("curl -s http://example.com | grep ' "Key" : * ' | cut -f1 -d " " | cut -b5- | rev | cut -b9- | rev") 

在 linux 中使用了以下内容并且工作正常。

PW = 'curl -s http://example.com | grep ' "Key" : * ' | cut -f1 -d " " | cut -b5- | rev | cut -b8- | rev'

我看到的错误是 SyntaxError: invalid syntax 它指向 "key" 部分

您可能只需要转义双引号。尝试:

os.system("curl -s http://example.com | grep ' \"Key\" : * ' | cut -f1 -d \" \" | cut -b5- | rev | cut -b9- | rev")