将python命令的输出写入文件

0 python

我不知道如何将命令“show tunneled-node-mgr tunneled-nodes”的输出写入文件 output.txt…………我尝试了以下操作,但不起作用

import subprocess
f=open("output.txt","w")
subprocess.Popen("show tunneled-node-mgr tunneled-nodes",stdout=file)
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是

File "1.py", line 3, in <module>
    subprocess.Popen("show tunneled-node-mgr tunneled-nodes",stdout=file)
  File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

ste*_*ver 5

指令

f=open("output.txt","w")
Run Code Online (Sandbox Code Playgroud)

创建一个名为f;的文件对象 然而

subprocess.Popen("show tunneled-node-mgr tunneled-nodes",stdout=file)
Run Code Online (Sandbox Code Playgroud)

尝试写入名为file-的对象,该对象不存在。