Reputation: 149
I just started using Google colab for my projects and I tried to create and parse text files. But I don't quite understand how the file directory works here. Below are my questions:
Appreciate your time, thank you.
Upvotes: 5
Views: 16957
Reputation: 5174
The google colab folders are temporary and they will disappear after 8 hours I think. You need to save them to your mounted google drive location. The content folder is part of colab and will be deleted.
You need to mount google drive to your Colab session.
from google.colab import drive
drive.mount('/content/gdrive')
Then you can write to google drive like
with open('/content/gdrive/My Drive/file.txt', 'w') as f:
f.write('content')
Or even save stuff like pandas files to csv there like
df.to_csv('/content/gdrive/My Drive/file.csv')
You can also read files from there like this
import pandas as pd
df = pd.read_csv('/content/gdrive/My Drive/file.csv')
All of this will only work after you mount the drive of course.
Upvotes: 11