how to open Text File in google Collab

21,797

Change to just

data_path = 'fra.txt'

Should work.

Share:
21,797
Sriram Arvind Lakshmanakumar
Author by

Sriram Arvind Lakshmanakumar

Software Engineer transitioning to Dev Ops Engineer

Updated on July 09, 2022

Comments

  • Sriram Arvind Lakshmanakumar
    Sriram Arvind Lakshmanakumar almost 2 years

    I am recently using google collab juypter notebook.After Uploading text file, unable to open the file using open function in python 3.

    from google.colab import files
    import io
    
    uploaded = files.upload()
    
    for fn in uploaded.keys():
      print('User uploaded file "{name}" with length {length} bytes'.format(
          name=fn, length=len(uploaded[fn])))
    data_path = io.StringIO(uploaded['fra.txt'].decode('utf-8'))
    with open(data_path, 'rb') as f:
        lines = f.read().split('\n')
    

    but it gives this error : TypeError: expected str, bytes or os.PathLike object, not _io.StringIO

    how to open text file in google collab juypter notebook ?

  • petegordon
    petegordon almost 6 years
    Worked. But watch out for the read bytes data type ('rb'); might want to use 'r' to bring in str data type. See example here... colab.research.google.com/drive/…