仅从python中的远程SFTP服务器下载新文件,忽略文件扩展名

发布于 2025-02-10 09:14:41 字数 1302 浏览 2 评论 0 原文

from datetime import datetime
import pysftp
import fnmatch
import os
from stat import S_IMODE, S_ISDIR, S_ISREG

Hostname = "Hostname"
Username = "Username"
Password = "Password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=Hostname, username=Username, password=Password,
                       cnopts=cnopts) as sftp:
    print("Connection successfully established ... ")

    local_dir = r"D:\testing"
    remote_dir = r'C:\Users\admin\Documents\personal'

    for entry in sftp.listdir(remote_dir):
        root, ext = os.path.splitext(entry)
        for entry1 in sftp.listdir(local_dir):
            root1, ext1 = os.path.splitext(entry1)
            if root == root1:
                if ext != ext1:
                    pass
            elif root != root1:
                remotepath = os.path.join(remote_dir, entry)
                localpath = os.path.join(local_dir, entry)
                sftp.get(remotepath, localpath, preserve_mtime=False)

我正在尝试将文件从SFTP服务器导出到本地。为此,我需要通过从服务器和本地文件夹中的文件名和文件扩展进行比较文件。

实例 abc.xlsx abc.csv adc.txt 从服务器和 本地文件夹已有 abc.xlsx ,那么我的脚本不应复制任何具有相同名称和相同扩展名或其他扩展名的文件。

在这里,我需要比较文件名和扩展名,

  • 如果同名,则不要下载
  • 同一名称和不同的扩展名,否
from datetime import datetime
import pysftp
import fnmatch
import os
from stat import S_IMODE, S_ISDIR, S_ISREG

Hostname = "Hostname"
Username = "Username"
Password = "Password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=Hostname, username=Username, password=Password,
                       cnopts=cnopts) as sftp:
    print("Connection successfully established ... ")

    local_dir = r"D:\testing"
    remote_dir = r'C:\Users\admin\Documents\personal'

    for entry in sftp.listdir(remote_dir):
        root, ext = os.path.splitext(entry)
        for entry1 in sftp.listdir(local_dir):
            root1, ext1 = os.path.splitext(entry1)
            if root == root1:
                if ext != ext1:
                    pass
            elif root != root1:
                remotepath = os.path.join(remote_dir, entry)
                localpath = os.path.join(local_dir, entry)
                sftp.get(remotepath, localpath, preserve_mtime=False)

I'm trying to export files from SFTP server to local. In order to do that I need to compare files by filename and file extension from server and local folder.

For instance abc.xlsx, abc.csv, adc.txt from server and
local folder has got abc.xlsx, then my script shouldn't copy any files with same name and same extension or different extension.

Here I need to compare the filenames and extension

  • if the same name then don't download
  • if same name and different extension don't download

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-02-17 09:14:41
  • 您必须为本地路径使用 os.listdir
  • 无需每次重新阅读本地文件夹(除非您以某种方式旨在满足远程目录中具有相同基本名称的多个文件))。
  • 您用于识别新文件的代码也不正确。
  • 您不应将 os.path.join 用于SFTP路径。

这应该做:

localfiles = os.listdir(local_dir)
for rentry in sftp.listdir(remote_dir):
    rroot, rext = os.path.splitext(rentry)
    found = False
    for lentry in localfiles:
        lroot, lext = os.path.splitext(lentry)
        if rroot == lroot:
            found = True
            break
    if not found:
        remotepath = remote_dir + "/" rentry
        localpath = os.path.join(local_dir, rentry)
        sftp.get(remotepath, localpath, preserve_mtime=False)

尽管这些天,但您不应使用PysFTP,因为它已经死了。直接使用Paramiko。参见 pysftp vs. paramiko 。上面的代码也将与paramiko一起使用 sftpclient.listdir


强制性警告:除非您不关心安全性,否则请勿设置 cnopts.hostkeys = none none 。有关正确的解决方案,请参见使用pysftpp 验证主机键。

  • You have to use os.listdir for the local path.
  • There's no need to re-read the local folder every time (unless you somehow aim to cater for multiple files with same base name in the remote directory).
  • Your code for identifying new files is not correct either.
  • You should not use os.path.join for SFTP paths.

This should do:

localfiles = os.listdir(local_dir)
for rentry in sftp.listdir(remote_dir):
    rroot, rext = os.path.splitext(rentry)
    found = False
    for lentry in localfiles:
        lroot, lext = os.path.splitext(lentry)
        if rroot == lroot:
            found = True
            break
    if not found:
        remotepath = remote_dir + "/" rentry
        localpath = os.path.join(local_dir, rentry)
        sftp.get(remotepath, localpath, preserve_mtime=False)

Though these days, you should not use pysftp, as it is dead. Use Paramiko directly instead. See pysftp vs. Paramiko. The above code will work with Paramiko too with its SFTPClient.listdir.


Obligatory warning: Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文