Working with Files
Duration: 5 min
This module delves into the essential skills needed to efficiently manage and manipulate files within Google Colab. Understanding how to work with files is crucial for data scientists and AI practitioners, as it enables seamless data ingestion, processing, and storage, which are foundational to any AI project.
Uploading and Accessing Files
Google Colab allows users to upload files directly from their local machine or access files stored in Google Drive. This capability is vital for loading datasets, models, and other resources necessary for AI projects. Files can be easily uploaded using the file browser interface or through the command line.
from google.colab import files
# Upload a file from your local machine
uploaded = files.upload()
# Access the uploaded file
for fn in uploaded.keys():
print(f'File name: {fn}', 'File size:', uploaded[fn].size, 'bytes')File name: example.txt File size: 1024 bytesReading and Writing Files
Once files are uploaded, they can be read and written using standard Python file I/O operations. Google Colab supports various file formats, including CSV, JSON, and text files. This flexibility allows for easy manipulation of data in different formats, which is essential for data preprocessing and model training.
import json
# Writing to a JSON file
data = {'name': 'John', 'age': 30}
with open('data.json', 'w') as f:
json.dump(data, f)
# Reading from the JSON file
with open('data.json', 'r') as f:
data_loaded = json.load(f)
print(data_loaded){'name': 'John', 'age': 30}💡 Tip: Always ensure that the file paths are correctly specified, especially when working with files stored in Google Drive. Use the full path to avoid any file access issues.
❓ How do you upload a file to Google Colab?
❓ Which function is used to read a JSON file in Google Colab?