How to give a command line command from python?
本问题已经有最佳答案,请猛点这里访问。
我从命令行中得到了一系列命令,我在命令行中调用了某些实用程序。明确地:
1 2 3 | root@beaglebone:~# canconfig can0 bitrate 50000 ctrlmode triple-sampling on loopback on root@beaglebone:~# cansend can0 -i 0x10 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 root@beaglebone:~# cansequence can0 -p |
我似乎不明白(或找不到清晰的文档)我是如何准确地编写一个Python脚本来发送这些命令的。我以前没有使用过操作系统模块,但怀疑这可能是我应该看的地方?
一个可以与子过程conveniently perform命令行命令的输出和检索:他们是否occurred年误差 >
1 2 3 4 5 6 7 8 9 10 11 | import subprocess def external_command(cmd): process = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out, err = process.communicate() errcode = process.returncode return errcode, out, err |
实例: >
1 | print external_command('ls -l') |
它应该是没问题的rearrange对等值的回报。 >
使用子过程 >
实例: >1 2 3 4 5 | >>> subprocess.call(["ls","-l"]) 0 >>> subprocess.call("exit 1", shell=True) 1 |