how to avoid shell=True in subprocess

后端 未结 2 1756
醉梦人生
醉梦人生 2020-12-21 08:45

I have subprocess command to check md5 checksum as

subprocess.check_output(\'md5 Downloads/test.txt\', stderr=subprocess.STDOUT, shell=True)
2条回答
  •  旧巷少年郎
    2020-12-21 09:43

    in case of complex commands , you can use shlex to pass the commands as a list to Check_Output or any other subprocess classes

    from the document

    shlex.split() can be useful when determining the correct tokenization for args, especially in complex cases:

    https://docs.python.org/3.6/library/subprocess.html#subprocess.check_output

    coming to above example

    import shlex
    inp="md5 Downloads/test.txt"
    command=shlex.split(inp)
    subprocess.check_output(command, stderr=subprocess.STDOUT)
    

提交回复
热议问题