简体   繁体   English

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

[英]Error while running linux command in Python using subprocess

The goal here is to get a key or p/w from a website. 这里的目标是从网站获取密钥或密码。

In Python, I have tried both os.system() and subprocess.call([]) to run following linux command but both method ends up with error. 在Python中,我尝试了os.system()和subprocess.call([])来运行以下linux命令,但是这两种方法均以错误结尾。

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

In linux following is being used and that works fine. 在linux中,使用了以下命令,效果很好。

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

The error I see is SyntaxError: invalid syntax and it points at the "key" part 我看到的错误是SyntaxError:语法无效,它指向“关键”部分

You probably just need to escape your double quotes. 您可能只需要转义双引号即可。 Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM