5

I'm trying to copy files from SFTP server . I can connect using python pysftp . I can run:

data = srv.listdir()
for i in data:
    print I

And I get the Directory list. But when I try

sftp.put (localpath,"file_name.txt")

I get

>"IOError: [Errno 13] Permission denied: 'C:\\....."

I have permission to that folder, because I can run MKDIR and it creates a directory in that file path. I have tried many many different ways but no luck so far, any help is truly appreciated.

import pysftp
import os

def sftpExample():
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None  
    
    with pysftp.Connection('HOST', username='username', password='Password', cnopts=cnopts) as sftp  :
        
        print 'connected '
        localpath="C:\\new project\\new"
        remotepath="/folder1"
        sftp.put(localpath,"infso.txt")
    
        sftp.put(localpath,remotepath)
        sftp.getfo (remotepath, localpath )
        srv.get_r(localpath,  remotepath)
        srv.close()
    
sftpExample() 

I get this error code:

Traceback (most recent call last):
File "db_backup.py", line 42, in <module>
sftpExample()
File "db_backup.py", line 17, in sftpExample
sftp.put(localpath,"GT-Dallas SFTP infso.txt")
File "c:\Python27\lib\site-packages\pysftp\__init_.py", line 364, in put
confirm=confirm)
File "c:\Python27\lib\site-packages\paramiko\sftp_client.py", line 720, in put
with open(localpath, 'rb') as fl:
IOError: [Errno 13] Permission denied: "C:\\new project\\new"

I've tried all different ways to copy the file as you see however I've had no luck so far.

Amit Pathak
  • 1,145
  • 11
  • 26
Saddem
  • 51
  • 1
  • 1
  • 2

2 Answers2

3

The issue is that you're trying to save a file as a directory which, at least in my experience, is what throws the Permission Denied error in pysftp.

Change this line of code:

localpath="C:\\new project\\new"

To this:

localpath="C:\\new project\\new\\infso.txt"

NOTE: infso.txt can be anything you want to name the local file being downloaded. I've used the same name here as the remote file's name from your example for symmetry and simplicity.

alphazwest
  • 3,483
  • 1
  • 27
  • 39
0

There are a few things that might be causing your issue, but the one that stands out to me comes from your error message:

IOError: [Errno 13] Permission denied: "C:\\new project\\new"

It might be that you need to escape the space ("\ ") or put it in a raw string r"C:\My Path With Spaces"

But in any case, I would avoid using spaces in your filenames, and you should rename your project folder to something like new_project or newproject.

Another thing that would make your life easier is if you compressed your directory into a single archive file (.tgz or .zip or something) and transfer that file.

N. Kern
  • 401
  • 2
  • 7