I am able to connect to the server and see all the files in a given directory, however, I seem to always run into problems when I try to open a file.
The scenario is: I want to get the latest file based on date from an SFTP server, then I want to check if that file has a particular string in it. If it does, return true, if not return false. Here is my solution below:
def does_file_exists_on_sftp_server_and_contains_given_value(value):
latest_date = 0
latest_file = None
retry_attempt = 0
value_is_present= False
while retry_attempt < 50:
# the line below does the sftp server login and that works successfully
conn = vm_sftp_login()
files = conn.listdir_attr("directory")
for file in files:
if file.filename.endswith(".xml") and file.st_mtime > latest_date:
latest_date = file.st_mtime
latest_file = file.filename
retry_attempt = 50
retry_attempt += 1
latest_file_obj = conn.get(latest_file)
file_obj = open(latest_file_obj)
for line in file_obj:
if value in line:
value_is_present = True
break
return value_is_present
The code seems to break at this point: latest_file_obj = conn.get(latest_file)
.
Traceback
self = <paramiko.sftp_client.SFTPClient object at 0x000001EF8E156748>
msg = paramiko.Message(b'\x00\x00\x00\x05\x00\x00\x00\x02\x00\x00\x00\x0cNo such file\x00\x00\x00\x00')
def _convert_status(self, msg):
"""
Raises EOFError or IOError on error status; otherwise does nothing.
"""
code = msg.get_int()
text = msg.get_text()
if code == SFTP_OK:
return
elif code == SFTP_EOF:
raise EOFError(text)
elif code == SFTP_NO_SUCH_FILE:
# clever idea from john a. meinel: map the error codes to errno
> raise IOError(errno.ENOENT, text)
E FileNotFoundError: [Errno 2] No such file