python - 在 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()
我没有收到错误,但什么也没发生。该文件夹已成功创建,但没有文件正在发送。有人有想法吗?多谢
解决方案
如果 Paramiko 没有抛出任何错误,则说明上传成功。该文件可能只是在与您想要/而不是您期望的位置不同的位置结束。
至少对于测试,如果不是永久的,请尝试绝对绝对路径。不要猜测,使用您在 GUI SFTP 客户端中看到的确切路径。
另一种可能性是服务器以某种方式自动处理上传的文件并将其移走。
推荐阅读
- python - Python比较文件名和文件夹名
- c# - 二进制搜索数组以在列表视图中显示一行字符串 c# (Visual Studio)
- javascript - 画布上的像素尺寸不合适
- sdk - 亚马逊设备消息传递 (ADM) 1.1.0 IncompatibleClassChangeError 异常
- java - 如何在创建和更改 UI 的方法的同时运行播放歌曲的方法?
- opencv - 无法通过视频帧进行对象检测 Tensorflow python
- git - Git文件夹重命名一直失败 - 权限被拒绝
- css - 引导下拉链接没有样式
- mongodb - 查询生成的没有模型的 mongo-version 集合
- javascript - 如何从代理对构造 JavaScript 中的 UTF-16 字符?