Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pysftp [Errno 13] Permission denied:

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.

like image 426
Saddem Avatar asked Oct 17 '22 04:10

Saddem


1 Answers

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.

like image 95
alphazwest Avatar answered Oct 20 '22 22:10

alphazwest