簡體   English   中英

使用子進程在Python中運行linux命令時出錯

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

這里的目標是從網站獲取密鑰或密碼。

在Python中,我嘗試了os.system()和subprocess.call([])來運行以下linux命令,但是這兩種方法均以錯誤結尾。

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:語法無效,它指向“關鍵”部分

您可能只需要轉義雙引號即可。 嘗試:

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