【发布时间】:2019-05-21 18:48:20
【问题描述】:
我正在编写一个 python 脚本来帮助我将文件从 sftp 服务器下载到我的本地文件夹。 当我运行脚本时,它只是下载空白文档 我继续尝试,但我失败了
我做了一个sftp服务器来测试代码,远程路径中指定的路径是服务器根目录
我在下面给出代码
import pysftp
myHostname = "192.168.56.1"
myUsername = "new45"
myPassword = "146515"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword,cnopts=cnopts) as sftp:
# Define the file that you want to download from the remote directory
remoteFilePath = 'C:\\Users\\Simon\\Desktop\\ftp\\testfile.txt'
# Define the local path where the file will be saved
# or absolute "C:\Users\sdkca\Desktop\TUTORIAL.txt"
localFilePath = 'C:\\Users\\Simon\\Desktop\\ftp2\\textfile.txt'
sftp.get(remoteFilePath, localFilePath)
# connection closed automatically at the end of the with-block'''
请告诉我为什么正在下载空白文件的错误
【问题讨论】:
-
该文件夹可能是 SFTP 服务器上的根目录。尝试从 remoteFilePath 中删除路径,然后将 testfile.txt 改为。如果这样不行,那么使用 CnOpts 上的 log 参数打开日志记录可能是值得的。