【发布时间】:2019-08-29 18:25:22
【问题描述】:
我正在使用 pysftp 上传远程处理的本地文件,然后返回到 SFTP,我可以在那里下载它。目前,我无法让代码识别已处理的远程文件已上传。尽管远程处理的文件已成功上传,但代码只会永远等待。
当代码运行时,如果我在 FileZilla 等服务上刷新连接,代码会立即识别出文件在其中并且运行良好。但是,我需要它在不手动刷新 FileZilla 的情况下工作。我不确定为什么代码无法识别出文件已上传并可以下载。
我已经尝试使用 while 语句断开连接然后重新连接到 SFTP,但没有成功。
import pysftp
import stat
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
srv = pysftp.Connection(host="host", username="username", password="passowrd", cnopts=cnopts)
print("Connection Made!")
# Put File In
srv.chdir(r'/path_to_remote_directory')
srv.put(r'C:\Path_to_local_file.csv')
print("File Uploaded")
while not srv.isfile(r'/Path_to_newly_uploaded_remote_file.csv'):
time.sleep(3)
print("Still Waiting")
if srv.isfile(r'/Path_to_newly_uploaded_remote_file.csv'):
print("File is Ready!")
srv.get('/Path_to_newly_uploaded_remote_file.csv', r'C:\Local_path_save_to/file.csv')
# Closes the connection
srv.close()
【问题讨论】:
-
/path_to_remote_directory和/Path_to_newly_uploaded_remote_file.csv是什么关系? -
你的 SFTP 服务器是什么?告诉我们Paramiko log file。
-
如果你用
listdir而不是stat来测试文件是否存在呢?