Note that there are some explanatory texts on larger screens.

plurals
  1. POParmiko SFTP File - Calling .next() causes StopIteration immediately, even with lines remaining
    primarykey
    data
    text
    <p>I'm attempting to use Paramiko (Python SSH library) to read a remote file, and iterate through the lines.</p> <p>My file looks something like this:</p> <pre><code># Instance Name VERSION COMMENT Bob 1.5 Bob the Builder Sam 1.7 Play it again, Sam </code></pre> <p>My Paramiko code looks something like this:</p> <pre><code>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, } </code></pre> <p>For some reason, when I run the above, I get a <code>StopIteration</code> error when I run .next() on the SFTP file handler:</p> <pre><code>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 </code></pre> <p>This is strange, because the instances textfile I'm reading has three lines in it - I'm using .next() to skip the header line.</p> <p>When I open the file locally, using Python's open(), .next() works fine.</p> <p>Also, I can iterate through the SFTP file handler fine, and it will print all three lines.</p> <p>And using .readline() instead of .next() seems to work fine as well - not sure why the .next() isn't playing nice.</p> <p>Is this some quirk of Paramiko's SFTP file handler, or am I missing something in the code above?</p> <p>Cheers, Victor</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload