Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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 Paramiko SFTP:将内容列表到文件_Python_Paramiko - Fatal编程技术网

Python Paramiko SFTP:将内容列表到文件

Python Paramiko SFTP:将内容列表到文件,python,paramiko,Python,Paramiko,正在尝试将列表内容写入远程文件。下面是我的代码 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) conn = ssh.connect(hostname='agnel-1', port=22, username='root', password='pass') stdin, stdout, stderr = ssh.exec_command("hostname") host =

正在尝试将列表内容写入远程文件。下面是我的代码

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn = ssh.connect(hostname='agnel-1', port=22, username='root', password='pass')
stdin, stdout, stderr = ssh.exec_command("hostname")
host = stdout.readlines()
diam_hostname = host[0].strip('\n')
gx_list = ["--hostname                                      {0}".format(diam_hostname),
           "--hostrealm                                     BNG-1.com",
           "--auth-app-id                                   16777238",
           "--verbose                                        1",
           "--port                                          3868",
           "--dictionary                                    r8-gx-standard",
           "--supported-vendor-id                           10415",
           "--supported-vendor-id                           8164",
           "--supported-vendor-id                           12645",
           "--gx-command 0",
           "--charging-rule-name                            prepaid",
           "--charg-service-id                              1",
           "--rating-grp                                    1",
           "--preemptive                                    1",
           "--disable-sctp"]
sftp = ssh.open_sftp()
try:
    f = sftp.open('{0}/Diam.gx'.format(self.inputs['minidiam_path']), 'w')
except IOError:
    print "Failed to Open the file for writing"
for s in gx_list:
    f.write(s + '\n')
f.close()
使用变量f获取错误

UnboundLocalError: local variable 'f' referenced before assignment
但是sftp打开文件时没有出现任何错误

谢谢, Agnel。

您是否也看到
“无法打开文件以写入打印出来的”
?如果您的代码引发了一个
IOError
,那么一旦由于您不返回而处理了异常,它就会执行其余语句。例如,以下代码引发错误
“NameError:未定义名称“f”

尝试:

除KeyError外:

    print "Keyerror"

打印f

您是否在IOError中得到该错误?没有得到任何IOError
    print "Keyerror"