How to store the result of an executed shell command in a variable in python?

前端 未结 5 1731
执笔经年
执笔经年 2020-12-13 06:20

I need to store the result of a shell command that I executed in a variable, but I couldn\'t get it working. I tried like:

import os    

call = os.system(\"         


        
5条回答
  •  星月不相逢
    2020-12-13 07:11

    All the other answers here are fine answers. In many situations, you do need to run external commands.

    This specific example has another option: you can read the file, process it line by line, and do something with the output.

    While this answer doesn't work for the "more general question being asked", I think it should always be considered. It is not always the "right answer", even where possible. Remembering this (easier), and knowing when (not) to apply it (more difficult), will make you a better programmer.

提交回复
热议问题