代码之家  ›  专栏  ›  技术社区  ›  singularity2047

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

  •  0
  • singularity2047  · 技术社区  · 6 年前

    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:无效语法,它指向“key”部分

    1 回复  |  直到 6 年前
        1
  •  3
  •   sacuL    6 年前

    你可能只需要避开你的双引号。尝试:

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