1

In my root folder I have few folders. For example:

root
  AAA
    a.txt
    a.xml
  BBB
   b.txt
   b.xml
  CCC
   c.xml
  DDD
   ......

I need to download all folder which contain .txt file inside it except DDD. For example AAA, BBB has .txt file so I need to download it except CCC(as CCC didn't contain .txt)

I used the below code which download all file except DDD but am not able to check whether it contains .txt or not

Code I tried:


from paramiko import SSHClient
import paramiko
from stat import S_ISDIR

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('host', username='test', password='123')


remote_dir ='/root'
local_path = 'C:/download/'

def download_dir(remote_dir, local_dir):
    import os
    os.path.exists(local_dir) or os.makedirs(local_dir)
    dir_items = sftp.listdir_attr(remote_dir)
    for item in dir_items:
        # assuming the local system is Windows and the remote system is Linux
        # os.path.join won't help here, so construct remote_path manually
        if(item.filename != "DDD"):
            remote_path = remote_dir + '/' + item.filename      
            local_path = os.path.join(local_dir, item.filename)
            
                    
                    if S_ISDIR(item.st_mode):
                        download_dir(remote_path, local_path)
                
                    else:
                
                        sftp.get(remote_path, local_path)
 

download_dir(remote_dir,local_path)

How can I check the files inside it and download the folder based on that?

sanazz
  • 35
  • 6

2 Answers2

0

You can use glob.glob(pathname, "*.txt") to get all txt files in a directory.

The documentation can be found here

0

Something like this:

import fnmatch
if S_ISDIR(item.st_mode):
    if any(fnmatch.fnmatch(f, "*.txt") for f in sftp.listdir(remote_path)):
        download_dir(remote_path, local_path)
else:
    sftp.get(remote_path, local_path)

It's bit inefficient, as you will list all subfolders twice. But you can optimize it easily, if needed.

Based on List files on SFTP server matching wildcard in Python using Paramiko.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992