Running Bash commands in Python

前端 未结 10 1637
礼貌的吻别
礼貌的吻别 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:52

    Also you can use 'os.popen'. Example:

    import os
    
    command = os.popen('ls -al')
    print(command.read())
    print(command.close())
    

    Output:

    total 16
    drwxr-xr-x 2 root root 4096 ago 13 21:53 .
    drwxr-xr-x 4 root root 4096 ago 13 01:50 ..
    -rw-r--r-- 1 root root 1278 ago 13 21:12 bot.py
    -rw-r--r-- 1 root root   77 ago 13 21:53 test.py
    
    None
    

提交回复
热议问题