调试python程序时,在调用shell命令用到subprocess.Popen进行调用shell接口。
遇到问题:
cmd = ['cd /tmp']
run_cmd_and_assert(cmd) -->>调用subprocess.Popen()
执行结果:ERROR: [Errno 2] No such file or directory: 'cd /tmp ': 'cd /tmp'
很奇怪,这里目录命名存在但是却报没有这个目录,百度后终于知道需要指定shell=True,即:
run_cmd_and_assert(cmd, shell=True) --->>>调用subprocess.Popen()时候,Popen函数指定shell=True,linux下参数executable将指定程序使用的shell,windows下无须指定。
关于subprocess.Popen网上很多资源,这里不作详细介绍。