Reputation: 1327
I'm using Paramiko to open a remote file, via sftp. The remote file has a list of phrases and I want to iterate through each line of the file to see if a given phrase matches one in the remote file.
Code used to get remote file:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(host, username=username, password=password)
self.sftp_client = self.ssh.open_sftp()
self.remote_file = self.sftp_client.open(remote_file_path, mode='rb')
def checkSubnet(self, phrase):
found = 0
for line in self.remote_file:
if phrase in line:
found = 1
print "FOUND IT"
break
return found
This will work for the first phrase matched, however if the next phrase to be matched is before the previous one in the file, then it won't find it. I've debugged this to the for loop starts again from where it previously broke in the last match. My understanding would be that it would start again at the top of the file.
Is there anyway to change this behaviour? Or even a better method of doing this. The file has about 97,000 phrases in it, and changes daily, so keeping a local version isn't possible.
Thanks
Upvotes: 1
Views: 361
Reputation: 20163
Put a seek(0)
call before the for
loop to go back to the beginning of the file:
def checkSubnet(self, phrase):
found = 0
self.remote_file.seek(0)
for line in self.remote_file:
if phrase in line:
found = 1
print "FOUND IT"
break
return found
Upvotes: 1