在Python中使用Paramiko上传文件似乎是可行的,但在服务器上找不到该文件

2024-09-20 04:00:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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()

我没有出错,但什么也没有发生。 文件夹已成功创建,但未发送任何文件。 有人有主意吗? 非常感谢


Tags: 文件thepathiptxtclientparamikoaddress
1条回答
网友
1楼 · 发布于 2024-09-20 04:00:24

如果Paramiko没有抛出任何错误,则上载成功。该文件的结尾位置可能与您想要/期望的位置不同

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


另一种可能是服务器会自动处理上传的文件并将其移开

相关问题 更多 >