Python Script execute commands in Terminal

前端 未结 9 1649
余生分开走
余生分开走 2020-11-29 16:51

I read this somewhere a while ago but cant seem to find it. I am trying to find a command that will execute commands in the terminal and then output the result.

For

相关标签:
9条回答
  • 2020-11-29 16:55
    • Custom standard input for python subprocess

    In fact any question on subprocess will be a good read

    • https://stackoverflow.com/questions/tagged/subprocess
    0 讨论(0)
  • 2020-11-29 16:59

    You could import the 'os' module and use it like this :

    import os
    os.system('#DesiredAction')
    
    0 讨论(0)
  • 2020-11-29 17:01

    I prefer usage of subprocess module:

    from subprocess import call
    call(["ls", "-l"])
    

    Reason is that if you want to pass some variable in the script this gives very easy way for example take the following part of the code

    abc = a.c
    call(["vim", abc])
    
    0 讨论(0)
  • 2020-11-29 17:02

    The os.popen() is pretty simply to use, but it has been deprecated since Python 2.6. You should use the subprocess module instead.

    Read here: reading a os.popen(command) into a string

    0 讨论(0)
  • 2020-11-29 17:02

    for python3 use subprocess

    import subprocess
    s = subprocess.getstatusoutput(f'ps -ef | grep python3')
    print(s)
    
    0 讨论(0)
  • 2020-11-29 17:06
    import os
    os.system("echo 'hello world'")
    

    This should work. I do not know how to print the output into the python Shell.

    0 讨论(0)
提交回复
热议问题