import shlex, subprocess


"""

1、subprocess.Popen("cat test.txt") # 因为默认shell=False,命令将不执行
2、subprocess.Popen("cat test.txt", shell=True) # 设置shell=True, 命令将执行
3、subprocess.Popen(["cat","test.txt"]) # 第一项被视为命令,其余为命令参数, 命令将执行
4、shlex.split(cmd)  #将字符串转列表形式
5、bash -c 'source /etc/profile && echo $PATH' # 测试需要执行的命令

"""
if __name__ == "__main__":
    cmd = "test.py" # 本脚本同级py文件  win: cmd = "test.py" linux : cmd = "python test.py"
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    stdout, stderr = p.communicate()
    print stdout

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2022-02-06
  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2022-12-23
  • 2021-04-26
  • 2022-12-23
  • 2021-08-25
相关资源
相似解决方案