Skip to main content
Question

Accessing files with Python

  • May 23, 2025
  • 2 replies
  • 69 views

Forum|alt.badge.img

Hello, 

I've been trying to use boxsdk to access files I have on box via Python. I'm am able to create a Client with a developer token and OAuth2 object. Then I try to get the file contents with a file_id and load things in with pandas with the following:

file_content = client.file(file_id).content(file_version=None, byte_range=None)

s=str(file_content,'utf-8')

data = StringIO(s)

df=pd.read_csv(data)

However the data frame is only populated with metadata. How can I access the full contents of the file? 
 

2 replies

Forum|alt.badge.img

Hi Katie,

I was not able to replicate your case, it may be related to streaming a big file, how big is your file?

So considering a simple csv file like:

itemType,itemID,name,description
folder,176840203842,Cenotes,
folder,176840211427,Eagle Ray Bay,
folder,176841144813,Ras Mohamed,
folder,176839738500,Sharks Bay,
folder,176838773123,Thistlegorm,
folder,175969946454,Tropicana,

This sample code:

def get_file_content(client:Client,file_id):
    file_content = client.file(file_id).content(file_version=None, byte_range=None)
    # file = client.file(file_id).get()
    # file_content = file.content(file_version=None, byte_range=None)
    s = str(file_content)
    data = StringIO(s)
    df = pd.read_csv(data)
    print(f"Data frame: {df}")
    return df

Produces this output, returning the data frame (note: I'm unfamiliar with pandas):

Data frame: Empty DataFrame
Columns: [b'itemType, itemID, name, description\r\n
folder, 176840203842, Cenotes, \r\n
folder, 176840211427, Eagle Ray Bay, \r\n
folder.1, 176841144813, Ras Mohamed, \r\n
folder.2, 176839738500, Sharks Bay, \r\n
folder.3, 176838773123, Thistlegorm, \r\n
folder.4, 175969946454, Tropicana, \r\n']
Index: []

You can take a look at the full example here.

Let me know if this helps or how we can explore this further.

 


Forum|alt.badge.img

I think it might be a permissions issue. I am currently an 'editor' on the folder I'm trying to access. Are there further permissions needed?