如何从服务器获取文件位置路径而不复制它?

2024-03-29 13:39:03 发布

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

我已经找到了我的NAS服务器,我可以在上面获取/上传文件。现在我有了一个需要从服务器读取.png文件位置并将其传递到UI线程上以显示图像的适合性。现在我只知道get方法需要本地位置来保存。我不想文件保存在我的本地计算机上,但我可以在我的应用程序上显示该图像。在

我已经看了这个http://docs.paramiko.org/en/2.1/api/sftp.html,但没有找到相关的方法来使用

代码是:-

import paramiko
paramiko.util.log_to_file(r'D:\TechnoThrone\Builds\paramiko.log')
# Open a transport
host = "stedgela01.TechnoThrone.com"
port = 2222
transport = paramiko.Transport((host, port))
# Auth
password = "xxx"
username = "xxxx"
transport.connect(username = username, password = password)
# Go!
sftp = paramiko.SFTPClient.from_transport(transport)
# Download
filepath = '/A/B/C/pic_ex.png'
localpath = r'D:\picfolder\pic_ex.png'
sftp.get(filepath, localpath)

Tags: 文件方法图像服务器loghostparamikoget
1条回答
网友
1楼 · 发布于 2024-03-29 13:39:03

我不太明白这个问题,所以我试着猜一猜。
您应该能够查看远程服务器路径中的内容,而无需在本地下载文件。
^如果不想下载,{a1}不是正确的方法,因为根据文档:

get(remotepath, localpath, callback=None) Copy a remote file (remotepath) from the SFTP server to the local host as localpath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience.

Parameters: remotepath (str) – the remote file to copylocalpath (str) – the destination path on the local host callback (callable) – optional callback function (form: func(int, int)) that accepts the bytes transferred so far and the total bytes to be transferred

还有其他方法可以在远程目录中获取文件名和那些不需要下载的属性。
例如,listdirlistdir_attr和{a4}。在

例如,listdir_attr

Return a list containing SFTPAttributes objects corresponding to files in the given path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the folder.

The returned SFTPAttributes objects will each have an additional field: longname, which may contain a formatted string of the file’s attributes, in unix format. The content of this string will probably depend on the SFTP server implementation.

Parameters: path (str) – path to list (defaults to '.')
Returns: list of SFTPAttributes objects

你可以从以下几点着手:

list_png_files = []
for file_attributes in sftp.listdir_attr("remote_path"):
    if file_attributes.filename.endswith('.png'):
        list_png_files.append(file_attributes.filename)

当然,检查一下它是否会给出相对路径或绝对路径。
类似的,您可以尝试使用listdir,等等

相关问题 更多 >