Parmiko SFTP 文件 - 调用 .next() 会立即导致 StopIteration,即使还有剩余行
我正在尝试使用 Paramiko(Python SSH 库)来读取远程文件,并遍历各行。
我的文件看起来像这样:
# Instance Name VERSION COMMENT
Bob 1.5 Bob the Builder
Sam 1.7 Play it again, Sam
我的 Paramiko 代码看起来像这样:
def get_instances_cfg(self):
'''
Gets a file handler to the remote instances.cfg file.
'''
transport = paramiko.Transport(('10.180.10.104', 22))
client = paramiko.SSHClient()
#client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('some_host', username='victorhooi', password='password')
sftp = client.open_sftp()
fileObject = sftp.file('/tmp/instances.cfg','r')
return fileObject
def get_root_directory(self):
'''
Reads the global instances.cfg file, and returns the instance directory.
'''
self.logger.info('Getting root directory')
instances_cfg = self.get_instances_cfg()
first_line = instances_cfg.next() # We skip the header row.
instances = {}
for row in instances_cfg:
name, version, comment = row.split(None, 2)
aeg_instances[name] = {
'version': version,
'comment': comment,
}
由于某种原因,当我运行上面的代码时,当我在 SFTP 文件处理程序上运行 .next() 时,我收到 StopIteration
错误:
first_line = instances_cfg.next() # We skip the header row.
File "/home/hooivic/python2/lib/python2.7/site-packages/paramiko/file.py", line 108, in next
raise StopIteration
StopIteration
这很奇怪,因为我正在读取的实例文本文件中有三行 - 我正在使用 .next() 跳过标题行。
当我在本地打开文件时,使用 Python 的 open()、.next() 工作正常。
另外,我可以很好地迭代 SFTP 文件处理程序,并且它将打印所有三行。
使用 .readline() 而不是 .next() 似乎也可以正常工作 - 不知道为什么 .next() 表现不佳。
这是 Paramiko 的 SFTP 文件处理程序的一些怪癖,还是我在上面的代码中遗漏了一些东西?
干杯, 胜利者
I'm attempting to use Paramiko (Python SSH library) to read a remote file, and iterate through the lines.
My file looks something like this:
# Instance Name VERSION COMMENT
Bob 1.5 Bob the Builder
Sam 1.7 Play it again, Sam
My Paramiko code looks something like this:
def get_instances_cfg(self):
'''
Gets a file handler to the remote instances.cfg file.
'''
transport = paramiko.Transport(('10.180.10.104', 22))
client = paramiko.SSHClient()
#client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('some_host', username='victorhooi', password='password')
sftp = client.open_sftp()
fileObject = sftp.file('/tmp/instances.cfg','r')
return fileObject
def get_root_directory(self):
'''
Reads the global instances.cfg file, and returns the instance directory.
'''
self.logger.info('Getting root directory')
instances_cfg = self.get_instances_cfg()
first_line = instances_cfg.next() # We skip the header row.
instances = {}
for row in instances_cfg:
name, version, comment = row.split(None, 2)
aeg_instances[name] = {
'version': version,
'comment': comment,
}
For some reason, when I run the above, I get a StopIteration
error when I run .next() on the SFTP file handler:
first_line = instances_cfg.next() # We skip the header row.
File "/home/hooivic/python2/lib/python2.7/site-packages/paramiko/file.py", line 108, in next
raise StopIteration
StopIteration
This is strange, because the instances textfile I'm reading has three lines in it - I'm using .next() to skip the header line.
When I open the file locally, using Python's open(), .next() works fine.
Also, I can iterate through the SFTP file handler fine, and it will print all three lines.
And using .readline() instead of .next() seems to work fine as well - not sure why the .next() isn't playing nice.
Is this some quirk of Paramiko's SFTP file handler, or am I missing something in the code above?
Cheers,
Victor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
next()
函数只是在内部调用readline()
。 唯一可能导致StopIteration
的情况是 readline 返回空字符串(查看代码,有 4 行)。查看您的文件的
readline()
返回值。如果它返回空字符串,则 paramiko 使用的行缓冲算法中一定存在错误。The the
next()
function is simply callingreadline()
internally. The only thing that can cause aStopIteration
is if readline returned an empty string (look at the code, it's 4 lines).Look at what the return of
readline()
is for your file. If it's returning an empty string, there has to be a bug in the line-buffering algorithm used by paramiko.