Python 将输出(函数?)打印到文件?
我有一个Python脚本,在这个脚本中,我登录到Cisco设备并希望将命令的输出打印到文件中 我的输出都正常,但不确定如何将其打印到文件中 这是我的代码,可以登录、打印所需的输出,然后注销。很好用——我知道它不优雅:) 我尝试添加一个函数并将函数的输出打印到文件中,但我认为我可能走错了方向Python 将输出(函数?)打印到文件?,python,function,output,Python,Function,Output,我有一个Python脚本,在这个脚本中,我登录到Cisco设备并希望将命令的输出打印到文件中 我的输出都正常,但不确定如何将其打印到文件中 这是我的代码,可以登录、打印所需的输出,然后注销。很好用——我知道它不优雅:) 我尝试添加一个函数并将函数的输出打印到文件中,但我认为我可能走错了方向 def cisco_output(): print policymap.before filename = "policymap.txt" target = open(filename, '
def cisco_output():
print policymap.before
filename = "policymap.txt"
target = open(filename, 'w')
target.write(cisco_output)
target.close()
不要在函数中
打印
,而是返回
要保存的内容。然后通过在函数名后面追加()
来调用函数
def cisco_output():
return policymap.before
filename = "policymap.txt"
target = open(filename, 'w')
target.write(cisco_output())
target.close()
不要在函数中
打印
,而是返回
要保存的内容。然后通过在函数名后面追加()
来调用函数
def cisco_output():
return policymap.before
filename = "policymap.txt"
target = open(filename, 'w')
target.write(cisco_output())
target.close()
不要在函数中
打印
,而是返回
要保存的内容。然后通过在函数名后面追加()
来调用函数
def cisco_output():
return policymap.before
filename = "policymap.txt"
target = open(filename, 'w')
target.write(cisco_output())
target.close()
不要在函数中
打印
,而是返回
要保存的内容。然后通过在函数名后面追加()
来调用函数
def cisco_output():
return policymap.before
filename = "policymap.txt"
target = open(filename, 'w')
target.write(cisco_output())
target.close()
with
不错,但问题是关于打印函数的结果,而不是对象属性。with
不错,但问题是关于打印函数的结果,而不是对象属性。with
不错,但问题是关于打印函数的结果,不是对象属性。与
结合很好,但问题是关于打印函数的结果,而不是对象属性。您使用的是哪种Python版本?您同时使用print
和print()
,这是不寻常的。您应该使用和来打开文件。您使用的是什么Python版本?您同时使用print
和print()
,这是不寻常的。您应该使用和来打开文件。您使用的是什么Python版本?您同时使用print
和print()
,这是不寻常的。您应该使用和来打开文件。您使用的是什么Python版本?您同时使用print
和print()
,这是不寻常的。您应该使用和
来打开文件。