How to Access Local Files in Google Colab


If you have a file or a set of files that you want to access as data in Google Colab, then you can try one of the below options.


Option 1: Using the Upload File Script

    from google.colab import files
    
    files_uploaded = files.upload()

    Once you add this to one of the cells of your Notebook, you will see "choose files" option, when you click on it, you will be able to upload files from your local (Windows/Linux/Mac) PC.

    Upload Local files to access on Google Colab
    file uploaded
    • data.txt(text/plain) - 18 bytes, last modified: n/a - 100% done
    
    Saving data.txt to data.txt

    Once the file(s) is uploaded, you will see it as logs and can be accessed using filename.


    Example of reading the just uploaded local file.
    with open("data.txt", "r") as file:
        content = file.read()
        print(content)

    1,100
    2,200
    3,300


Option 2: Access Files from Google Drive

    One other way is to upload your local files to Google Drive and access them by mounting them using the below code.

    from google.colab import drive
    drive.mount('/content/drive')
    

This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Sonoma 14.0.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap