Skip to main content
Question

Want to Download contents of a folder

  • May 23, 2025
  • 1 reply
  • 23 views

Forum|alt.badge.img

Hi Team

iI am new to Box REST API.  Is it possible to download folder and all of its sub folder/files in 1 call ? 

if yes, whats the code for that in Python?

I checked available docs and at link https://developer.box.com/guides/downloads/folder/

I can see there is some code to download folder contents but thats in .Net, Java and Node.

Do we have something in python?

 

Thanks and Regards

Deepak Kumar

1 reply

Forum|alt.badge.img

Hi Deepak,

You have 2 options:

Download the folder as a zip file:

from boxsdk import JWTAuth, Client
def main():
auth = JWTAuth.from_settings_file('.jwt.config.json')
auth.authenticate_instance()
client = Client(auth)
folder_id = '163422716106'
user = client.user().get()
print(f"User: {user.id}:{user.name}")
folder = client.folder(folder_id).get()
print(f"Shared Folder: {folder.id}:{folder.name}")
print("#" * 80)
print("Type\tID\t\tName")
items = folder.get_items()

download_items=[]
for item in items:
print(f"{item.type}\t{item.id}\t{item.name}")
download_items.append(item)

print("#" * 80)

with open(folder.name+'.zip', "wb") as download_file:
status = client.download_zip(folder.name, download_items, download_file)

print(f"Download status: {status}")

if __name__ == "__main__":
main()
print("Done")

Resulting in:

User: 20344589936:UI-Elements-Sample
Shared Folder: 163422716106:Box UI Elements Demo
################################################################################
Type    ID              Name
folder  168248338385    test
folder  163436720758    Uploads
file    960391244732    Box-unsplash.jpg
file    1031018607151   test.txt
################################################################################
Download status: {'total_file_count': 19, 'downloaded_file_count': 19, 'skipped_file_count': 0,
'skipped_folder_count': 0, 'state': 'succeeded', ...}
Done
(file in folder 15M Feb  3 10:21 Box UI Elements Demo.zip)

The other option is to recursively download each item one by one:

import os
from boxsdk import JWTAuth, Client

def main():
auth = JWTAuth.from_settings_file('.jwt.config.json')
auth.authenticate_instance()
client = Client(auth)
folder_id = '163422716106'
user = client.user().get()
print(f"User: {user.id}:{user.name}")
folder = client.folder(folder_id).get()
print(f"Downloadin folder: {folder.id}:{folder.name}")
print("#" * 80)
print("Type\tID\t\tName")
os.chdir('downloads')

items = folder.get_items()
download_items(items)
os.chdir('..')

def download_items(items):
for item in items:
if item.type == 'folder':
os.mkdir(item.name)
os.chdir(item.name)
download_items(item.get_items())
os.chdir('..')
if item.type == 'file':
print(f"{item.type}\t{item.id}\t{item.name}",end='')
with open(item.name,'wb') as download_file:
item.download_to(download_file)
print("\tdone")

if __name__ == "__main__":
main()
print("Done")

 Resulting in:

User: 20344589936:UI-Elements-Sample
Downloadin folder: 163422716106:Box UI Elements Demo
################################################################################
Type    ID              Name
file    992001466707    7091F798-2502-40A9-B2A2-0561FC3FA84B.jpeg       done
file    992008414626    A4794839-9F81-43BE-B3EE-B7D3BF07E8E5.webp       done
file    962025972260    Audio.mp3       done
file    960488030766    AWS Prime - Pre-Requisites.pdf  done
file    960483183051    AWS Prime Workshop.pdf  done
...
Done

Hope this helps.