Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将输出(函数?)打印到文件?_Python_Function_Output - Fatal编程技术网

Python 将输出(函数?)打印到文件?

Python 将输出(函数?)打印到文件?,python,function,output,Python,Function,Output,我有一个Python脚本,在这个脚本中,我登录到Cisco设备并希望将命令的输出打印到文件中 我的输出都正常,但不确定如何将其打印到文件中 这是我的代码,可以登录、打印所需的输出,然后注销。很好用——我知道它不优雅:) 我尝试添加一个函数并将函数的输出打印到文件中,但我认为我可能走错了方向 def cisco_output(): print policymap.before filename = "policymap.txt" target = open(filename, '

我有一个Python脚本,在这个脚本中,我登录到Cisco设备并希望将命令的输出打印到文件中

我的输出都正常,但不确定如何将其打印到文件中

这是我的代码,可以登录、打印所需的输出,然后注销。很好用——我知道它不优雅:)

我尝试添加一个函数并将函数的输出打印到文件中,但我认为我可能走错了方向

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()
,这是不寻常的。您应该使用
来打开文件。