python - 使用 pysftp 从 SFTP 服务器下载文件到本地计算机时出现 PermissionError

标签 python sftp pysftp

我无法从 SFTP 服务器复制文件。我收到权限错误。

我尝试更改 os.mkdirs() 函数中的模式。

sftp = pysftp.Connection(host = myhostname, username= myusername,  password= mypassword, port=22, cnopts=mycn`enter code here`opts)
os.makedirs('S:\\work\\Automation\\ML\\Procressed data\\FY20\\FW'+fweek+'\\output', exist_ok=True)

localpath = 'S:\\work\\Automation\\ML\\Procressed data\\FY20\\'+fweek+'\\output' 
englishpath ='dell.ccaprocessrepository/output_ts_2020'+fweek+'_English_EU.txt'
chinapath = 'repository/output_ts_2020'+fweek+'_CHT.txt'
japanpath = 'repository/output_ts_2020'+fweek+'_Japan.txt'
koreapath = 'repository/output_ts_2020'+fweek+'_Korea.txt'

sftp.get(remotepath= englishpath, localpath= localpath)
sftp.get(remotepath= chinapath, localpath= localpath)
sftp.get(remotepath= japanpath, localpath= localpath)
sftp.get(remotepath= koreapath, localpath= localpath)
PermissionError: [Errno 13] Permission denied: 'S:\\work\\Automation\\ML\\Procressed data\\FY20\\29\\output'

最佳答案

pysftp 的 localpath 参数 Connection.get method文件的路径,下载的内容应存储在其中。

本地路径中的输出是一个文件夹,而不是文件

您需要将文件名附加到路径。

englishlocalpath = localpath + '\\output_ts_2020'+fweek+'_English_EU.txt'
sftp.get(remotepath=englishpath, localpath=englishlocalpath)

或者,将本地工作目录更改为 localpath 并且可以省略 localpath 参数。 pysftp 会将文件下载到工作目录,并保留原始名称(取自 remotepath 参数):

os.chdir(localpath)

sftp.get(remotepath=englishpath)
sftp.get(remotepath=chinapath)
sftp.get(remotepath=japanpath)
sftp.get(remotepath=koreapath)

关于python - 使用 pysftp 从 SFTP 服务器下载文件到本地计算机时出现 PermissionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57771997/

相关文章:

Python RPi - 从另一个脚本运行脚本时找不到文件

python - 使用数组切片时的问题

laravel - 使 PHPStorm 文件与通过 php artisan 在服务器上生成的文件保持同步

PowerShell 和 WinSCP 脚本检查 SFTP 远程目录中是否有超过 4 个文件

使用 pysftp 通过 HTTP 代理进行 Python 连接

python - 如何使用 Django Check Constraint 在数据库级别限制变量?

python - 如何平滑Python中的数据?

c# - SSH.Net异步文件下载

支持 ssh-dss 公钥算法的 Python FTP 实用程序

python - 使用 python pysftp 包,出现 "SSHException: Bad host key from server"错误