0

I'm fairly new to Python and migrating an already running script to a new machine. The key part of the script is to ftp a file to internal file server. A snippet of code is:

import pysftp
with pysftp.connection(host="hostname",username="uname",password="pwd") as sftp:
    sftp.put("d://folder1//file1.txt","file1.txt")

this code throws the following Error:


Traceback (most recent call last):
 File "<stdin>", line 2, in <module>
 File "folder//sftp.py", line 349, in put confirm=confirm)
 File "...\paramiko\sftp_client.py", line 669 in put return self.putfo(f1, remotepath, file_size, callback, confirm)
 File "...\paramiko\sftp_client.py", line 633 in putfo s = self.stat(remotepath) 
 File "...\paramiko\sftp_client.py", line 413 in stat t, msg = self.request(CMD_STAT, path)
 File "...\paramiko\sftp_client.py", line 729 in _request return self.read_response(num)
 File "...\paramiko\sftp_client.py", line 776 in _read_response self.convert_status(msg)
 File "...\paramiko\sftp_client.py", line 802 in _convert_status raise IOError(errno.ENOENT, text)
IOError: [Errno 2] No such file

However, the ftp file can be accessed succesfully as below:

f= open("d://folder//file1.txt",r)
print (f.read())

have you seen such error? any idea how to overcome this error?

Appreciate your help, Thanks

KS_23
  • 11
  • 3
  • What happens when you provide a full absolute path as a raw string, i.e.: `sftp.put(r"C:\Users\KS23\repos\folder\file1.txt")`? Also, could it be that it's not complaining about the local file not existing, but that there's something with the remote folder you're trying to upload to? You could try to [follow the example](https://pysftp.readthedocs.io/en/release_0.2.9/) including the `with sftp.cd("some_folder"):` part. – Saaru Lindestøkke Jun 19 '23 at 16:54
  • Thanks Saaru. I did attempt earlier with full path, just i didnt type it well in the question and i have corrected it now. I did try the example you gave but still the same error msgs. – KS_23 Jun 20 '23 at 09:12
  • 1
    Thanks Martin. I corrected the question now with full path. I'm attempting to transfer a local file to remote server. The open file is to show that Python can read the local file but on sftp, it fails with IO error(above). This is mgration and in the current setup, the remote pc does recieve file on a daily basis but i see your point on trying via gui. let me try now. – KS_23 Jun 20 '23 at 09:24

1 Answers1

0

I think it can help:)

import pysftp
with pysftp.Connection(host="hostname", username="uname", password="pwd") as sftp:
    local_path = "d://folder1//file1.txt"
    remote_path = "/path/to/remote/directory/file1.txt"  # replace with real remote path
    sftp.put(local_path, remote_path)