简体   繁体   中英

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.

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.

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

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") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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