Getting WindowsError when trying ot copy a file from one directory to another w/ paramiko
Question: Good afternoon,

I am getting the following error whenever I am trying to copy a test file from
one directory to another on a remote server:

**Traceback (most recent call last): File "", line 1, in File
"C:\Python27\lib\site-
packages\paramiko-1.12.0-py2.7.egg\paramiko\sftp_client.py", line 612, in put
file_size = os.stat(localpath).st_size WindowsError: [Error 3] The system
cannot find the path specified: '/brass/prod/bin/chris/test1/km_cust'**

The file I am looking to copy is called km_cust.

I am executing these commands in python 2.7.

Please note that the hostname, uid, and password were changed to generic
versions and the real hostname, uid, and password can be used to ssh to the
box in question and preform all functionality.

Here is my code:

    
    
    import paramiko
    
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect('hostname',username='test',password='pw')
    filepath = '/brass/prod/bin/chris/test1/km_cust'
    localpath = 'brass/prod/bin/chris/test2'
    
    sftp = s.open_sftp()
    sftp.put(filepath, localpath)
    

Any help will be apprecaited. Let me know if any other information is needed.


Answer: The problem is that [`put`](http://docs.paramiko.org) copies a _local_
file—that is, a file n your Windows box—to the server. As the documentation
says:

    
    
    put(self, localpath, remotepath, callback=None, confirm=True)
    Copy a local file (localpath) to the SFTP server as remotepath.
    

Note that you're also specifying (or at least naming) the paths backward… but
that doesn't really matter here, because neither one is _actually_ a local
path. So when you do this:

    
    
    sftp.put(filepath, localpath)
    

… it's looking for a file named `'/brass/prod/bin/chris/test1/km_cust'` on
your Windows box, and of course it can't find such a file.

If you want to copy a remote file to a different remote file, you need to do
something like this:

    
    
    f = sftp.open(filepath)
    sftp.putfo(f, localpath)
    

Or:

    
    
    f = sftp.open(localpath, 'wx')
    sftp.getfo(filepath, f)
    

* * *

Also, I'm guessing your `filepath` is supposed to start with a `/`.

* * *

However, this probably isn't what you wanted to do in the first place. Copying
a file from the remote server to the remote server via sftp involves
downloading all of the bytes to your Windows machine and then uploading them
back to the remote machine. A better solution would be to just tell the
machine to do the copy itself:

    
    
    s.exec_command("cp '{}' '{}'".format(filepath, localfile))
    s.close()
    

Note that in anything but the most trivial of cases, you're going to have to
deal with the `Channel` and its in/out/err and wait on its exit status. But I
believe that for this case, you should be fine.

