Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python从SFTP服务器下载时不要下载空文件夹_Python_Python 3.x_Sftp_Paramiko_Pysftp - Fatal编程技术网

使用Python从SFTP服务器下载时不要下载空文件夹

使用Python从SFTP服务器下载时不要下载空文件夹,python,python-3.x,sftp,paramiko,pysftp,Python,Python 3.x,Sftp,Paramiko,Pysftp,我在这个网站上得到了一个用Python递归下载文件的代码。此代码还下载服务器上的空目录 请帮助我修改此代码,使其不会从服务器下载空目录 我拥有的代码(基于): 您可以延迟创建本地目录,直到遇到要下载的文件: from stat import S_ISDIR, S_ISREG 试试这个 import os import pysftp from stat import S_IMODE, S_ISDIR, S_ISREG cnopts = pysftp.CnOpts() cnopts.hostke

我在这个网站上得到了一个用Python递归下载文件的代码。此代码还下载服务器上的空目录

请帮助我修改此代码,使其不会从服务器下载空目录

我拥有的代码(基于):


您可以延迟创建本地目录,直到遇到要下载的文件:

from stat import S_ISDIR, S_ISREG
试试这个

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

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None    
sftp=pysftp.Connection('192.168.X.X', username='username',password='password',cnopts=cnopts)

def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
    no_of_entries=0;
    for entry in sftp.listdir(remotedir):
        remotepath = remotedir + "/" + entry
        localpath = os.path.join(localdir, entry)
        mode = sftp.stat(remotepath).st_mode
        if S_ISDIR(mode):
            try:
                os.mkdir(localpath,mode=777)
            except OSError:
                pass
            sub = get_r_portable(sftp, remotepath, localpath, preserve_mtime);
            if(sub==0):
                os.rmdir(localpath);
            else:
                no_of_entries+=sub;
        elif S_ISREG(mode):
            sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime);
            no_of_entries+=1;
    return no_of_entries;

remote_path=input("enter the remote_path: ")
local_path=input("enter the local_path: ")

get_r_portable(sftp, remote_path, local_path, preserve_mtime=False)

在某些情况下,如果存在类似/tmp/a/b/c的文件,则此代码将失败/file@GuruVishnuVardhanReddy好的,没错。我已经解决了。是的,使用exist\u ok=True的os.makedirs可以很好地工作,而不是os.mkdir
def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
    for entry in sftp.listdir(remotedir):
        remotepath = remotedir + "/" + entry
        localpath = os.path.join(localdir, entry)
        mode = sftp.stat(remotepath).st_mode
        if S_ISDIR(mode):
            get_r_portable(sftp, remotepath, localpath, preserve_mtime)
        elif S_ISREG(mode):
            os.makedirs(localdir, exist_ok=True)
            sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)
import os
import pysftp
from stat import S_IMODE, S_ISDIR, S_ISREG

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None    
sftp=pysftp.Connection('192.168.X.X', username='username',password='password',cnopts=cnopts)

def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
    no_of_entries=0;
    for entry in sftp.listdir(remotedir):
        remotepath = remotedir + "/" + entry
        localpath = os.path.join(localdir, entry)
        mode = sftp.stat(remotepath).st_mode
        if S_ISDIR(mode):
            try:
                os.mkdir(localpath,mode=777)
            except OSError:
                pass
            sub = get_r_portable(sftp, remotepath, localpath, preserve_mtime);
            if(sub==0):
                os.rmdir(localpath);
            else:
                no_of_entries+=sub;
        elif S_ISREG(mode):
            sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime);
            no_of_entries+=1;
    return no_of_entries;

remote_path=input("enter the remote_path: ")
local_path=input("enter the local_path: ")

get_r_portable(sftp, remote_path, local_path, preserve_mtime=False)