Subscribe
Accepted Solution

Iterating over directory tree (recursively)

Hi community,

I'm trying to fetch all the file inside a given directory recursively. Are there any easy way to implement this using the API?

example.py

import sys

sys.path.append('[omitted output]')

from NaServer import *

s = NaServer('[omitted output]', 1 , 20)

s.set_server_type('FILER')

s.set_transport_type('HTTPS')

s.set_port(443)

s.set_style('LOGIN')

s.set_vserver('[omitted output]')

s.set_admin_user('[omitted output]', '[omitted output]')

volpath = '/vol/[omitted output]'

api = NaElement('file-list-directory-iter')

api.child_add_string('path',volpath)

xi = NaElement('desired-attributes')

api.child_add(xi)

xi1 = NaElement('file-info')

xi.child_add(xi1)

xi1.child_add_string('name','<name>')

xi1.child_add_string('file-type','<file-type>')

xo = s.invoke_elem(api)

if xo.results_status() == 'failed':

    print 'Error:\n'

    print xo.sprintf()

    sys.exit(1)

print 'VMDK files on '+volpath+':\n'

for file in xo.child_get('attributes-list').children_get():

     if 'flat.vmdk' in file.child_get_string('name'):

          print file.child_get_string('name')

Thanks in advance!

Re: Iterating over directory tree (recursively)

I found the answer using this same logic:

import os

def list_files(path):

          for i in os.listdir(path):

                    if os.path.isdir(path+'/'+i):

                              list_files(path+'/'+i)

                    else:

                              print path+'/'+i