python powershell;

执行power shell命令并显示输出

  1. import subprocess
  2. cmd = "ls"
  3. # cmd1 能够借助power shell保存输出到txt,但是不会显示输出
  4. cmd1 = "ls > result.txt"
  5. # cmd2 能够借助power shell保存输出到txt,同时显示输出结果
  6. cmd2 = "ls > result.txt ; cat result.txt"
  7. subprocess.run(["powershell", "-Command", cmd], stdout=sys.stdout)