0

I have a strange behaviour when trying to upload a file into an sftp by using paramiko.

myHostname = "xxx"
port = 22
myUsername = "yyy"
myPassword = "zzz"
try:
  t = paramiko.Transport((myHostname, port))
  t.connect(username = myUsername, password = myPassword)
  sftp = paramiko.SFTPClient.from_transport(t) 
  sftp.put("C:\Users\simeone\Documents\Repository\REC_bbgAIM\f6431recupload.csv.TEMPL_BNP_MIL_CASH.inc","/f6431recupload.csv.TEMPL_BNP_MIL_CASH.inc")   
### Comment: you see that as first argument I passed the whole path with \, result does not change if I use / or \\
finally:
  sftp.close()
  t.close()

I get the following error:

Traceback (most recent call last):
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\main.py", line 25, in <module>
    sftp.put("f6431recupload.csv.TEMPL_BNP_MIL_CASH.inc", "/f6431recupload.csv.TEMPL_BNP_MIL_CASH.inc")
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\REC_AIM_bbg\Lib\site-packages\paramiko\sftp_client.py", line 759, in put
    return self.putfo(fl, remotepath, file_size, callback, confirm)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\REC_AIM_bbg\Lib\site-packages\paramiko\sftp_client.py", line 720, in putfo
    s = self.stat(remotepath)
        ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\REC_AIM_bbg\Lib\site-packages\paramiko\sftp_client.py", line 493, in stat
    t, msg = self._request(CMD_STAT, path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\REC_AIM_bbg\Lib\site-packages\paramiko\sftp_client.py", line 822, in _request
    return self._read_response(num)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\REC_AIM_bbg\Lib\site-packages\paramiko\sftp_client.py", line 874, in _read_response
    self._convert_status(msg)
  File "C:\Users\simeone\Documents\Repository\REC_bbgAIM\REC_AIM_bbg\Lib\site-packages\paramiko\sftp_client.py", line 903, in _convert_status
    raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] No such file

I checked references:

Upload all files from local folder with specific extension to SFTP server using Paramiko

Paramiko SFTPClient.get fails with "FileNotFoundError: [Errno 2] No such file or directory" referring to the remote file path

The error happens in the put, but the behaviour I get is very strange because sometimes the file is uploaded correctly although I get the error message.

I work on Windows and python 3.11.1 I tried with pysftp as well, but was not working and thought was due to the fact that is no longer maintained.

Dark2018
  • 585
  • 1
  • 5
  • 7

0 Answers0