Search code examples
pythongoogle-colaboratory

How can access Uploaded File in Google colab


I'm new in python and I use Google Colab . I uploaded a train_data.npy into google Colab and then I want to use it . According to this link How to import and read a shelve or Numpy file in Google Colaboratory?

when i run my code i face this error :

TypeError: 'dict_keys' object does not support indexing

Here is my code :

uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

with open('train_data.npy', 'w') as f:
f.write(uploaded[uploaded.keys()[0]])

Thanks


Solution

  • Here's an adjustment to your snippet that will save any uploaded file in the current directory using the uploaded file name.

    from google.colab import files
    uploaded = files.upload()
    
    for name, data in uploaded.items():
      with open(name, 'wb') as f:
        f.write(data)
        print ('saved file', name)