Running Bash commands in Python

前端 未结 10 1625
礼貌的吻别
礼貌的吻别 2020-11-21 05:16

On my local machine, I run a python script which contains this line

bashCommand = \"cwm --rdf test.rdf --ntriples > test.nt\"
os.system(bashCommand)
         


        
10条回答
  •  伪装坚强ぢ
    2020-11-21 05:36

    The pythonic way of doing this is using subprocess.Popen

    subprocess.Popen takes a list where the first element is the command to be run followed by any command line arguments.

    As an example:

    import subprocess
    
    args = ['echo', 'Hello!']
    subprocess.Popen(args) // same as running `echo Hello!` on cmd line
    
    args2 = ['echo', '-v', '"Hello Again"']
    subprocess.Popen(args2) // same as running 'echo -v "Hello Again!"` on cmd line
    

提交回复
热议问题