首页 > 解决方案 > 在 Python 中使用 Paramiko 上传文件看似可行,但在服务器上找不到文件

问题描述

我是 Python 新手,很抱歉我的英语不好。我正在尝试将文件“toto.txt”从我的硬盘“d:”保存到我的 Synology NAS。所以我将为此使用 paramiko,这是代码:

import paramiko
import os

ip_address = "my nas ip"
username = "my username"
password = "mypass"
utilisateur = os.getenv("USERNAME") // to get Windows username
remote_path = f"{utilisateur}/test.txt" // the file downloaded
local_path = "d:/toto.txt" //the file stored on my pc

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print("Connexion OK >", ip_address)

stdin, stdout, stderr = ssh_client.exec_command(f'mkdir {utilisateur}') //creating folder for the 
user
sftp = ssh_client.open_sftp()
sftp.put(local_path,remote_path) // trying to send the file
sftp.close()
ssh_client.close()

我没有收到错误,但什么也没发生。该文件夹已成功创建,但没有文件正在发送。有人有想法吗?多谢

标签: pythonfilesshsftpparamiko

解决方案


如果 Paramiko 没有抛出任何错误,则说明上传成功。该文件可能只是在与您想要/而不是您期望的位置不同的位置结束。

至少对于测试,如果不是永久的,请尝试绝对绝对路径。不要猜测,使用您在 GUI SFTP 客户端中看到的确切路径。


另一种可能性是服务器以某种方式自动处理上传的文件并将其移走。


推荐阅读