code: def copyHdfsDirectoryToLocal(hdfsDirectory, localDirectory, hdfsClient):
      '''Copy directory from HDFS to local'''
      if not os.path.exists(localDirectory):
          os.makedirs(localDirectory)
      try:
          listing = hdfsClient.list_status(hdfsDirectory)
      except Exception as exception:
          nni_log(LogType.Error, 'List hdfs directory {0} error: {1}'.format(hdfsDirectory, str(exception)))
          raise exception
  
      for f in listing:
          if f.type == 'DIRECTORY':
              subHdfsDirectory = posixpath.join(hdfsDirectory, f.pathSuffix)
              subLocalDirectory = os.path.join(localDirectory, f.pathSuffix)
              copyHdfsDirectoryToLocal(subHdfsDirectory, subLocalDirectory, hdfsClient)
          elif f.type == 'FILE':
              hdfsFilePath = posixpath.join(hdfsDirectory, f.pathSuffix)
              localFilePath = os.path.join(localDirectory, f.pathSuffix)            
              copyHdfsFileToLocal(hdfsFilePath, localFilePath, hdfsClient)
          else: 
              raise AssertionError('unexpected type {}'.format(f.type)f copyHdfsDirectoryToLocal(hdfsDirectory, localDirectory, hdfsClient):
      '''Copy directory from HDFS to local'''
      if not os.path.exists(localDirectory):
          os.makedirs(localDirectory)
      try:
          listing = hdfsClient.list_status(hdfsDirectory)
      except Exception as exception:
          nni_log(LogType.Error, 'List hdfs directory {0} error: {1}'.format(hdfsDirectory, str(exception)))
          raise exception
  
      for f in listing:
          if f.type == 'DIRECTORY':
              subHdfsDirectory = posixpath.join(hdfsDirectory, f.pathSuffix)
              subLocalDirectory = os.path.join(localDirectory, f.pathSuffix)
              copyHdfsDirectoryToLocal(subHdfsDirectory, subLocalDirectory, hdfsClient)
          elif f.type == 'FILE':
              hdfsFilePath = posixpath.join(hdfsDirectory, f.pathSuffix)
              localFilePath = os.path.join(localDirectory, f.pathSuffix)            
              copyHdfsFileToLocal(hdfsFilePath, localFilePath, hdfsClient)
          else: 
              raise AssertionError('unexpected type {}'.format(f.type))
docs: Copy directory from HDFS to local