简体   繁体   English

python pysftp [Errno 13] 权限被拒绝:

[英]python pysftp [Errno 13] Permission denied:

I'm trying to copy files from SFTP server .我正在尝试从 SFTP 服务器复制文件。 I can connect using python pysftp .我可以使用 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:\\....." “IOError:[Errno 13] 权限被拒绝:'C:\\.....”

I have permission to that folder, because I can run MKDIR and it creates a directory in that file path.我对该文件夹有权限,因为我可以运行 MKDIR 并在该文件路径中创建一个目录。 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.如您所见,我已经尝试了所有不同的方法来复制文件,但是到目前为止我还没有运气。

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 .问题是您正在尝试将文件保存为目录,至少在我的经验中,这是在pysftp引发Permission Denied错误的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.注意: infso.txt可以是任何你想命名正在下载的本地文件。 I've used the same name here as the remote file's name from your example for symmetry and simplicity.为了对称和简单起见,我在这里使用了与您示例中的远程文件名称相同的名称。

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"可能是您需要转义空格 ( "\\ " ) 或将其放入原始字符串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 .但无论如何,我会避免在文件名中使用空格,并且您应该将项目文件夹重命名为new_projectnewproject

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.另一件让您的生活更轻松的事情是,如果您将目录压缩为单个存档文件( .tgz.zip或其他文件)并传输文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM