War*_*aud 4 python google-colaboratory
我刚刚开始在我的项目中使用 Google colab,并尝试创建和解析文本文件。但我不太明白文件目录在这里是如何工作的。以下是我的问题:
感谢您的宝贵时间,谢谢。
google colab 文件夹是临时的,我认为它们会在 8 小时后消失。您需要将它们保存到您安装的谷歌驱动器位置。content 文件夹是 colab 的一部分,将被删除。
您需要将 google Drive 安装到您的 Colab 会话中。
from google.colab import drive
drive.mount('/content/gdrive')
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样写入谷歌驱动器
with open('/content/gdrive/My Drive/file.txt', 'w') as f:
f.write('content')
Run Code Online (Sandbox Code Playgroud)
或者甚至将 pandas 文件等内容保存到 csv 中,例如
df.to_csv('/content/gdrive/My Drive/file.csv')
Run Code Online (Sandbox Code Playgroud)
您还可以像这样从那里读取文件
import pandas as pd
df = pd.read_csv('/content/gdrive/My Drive/file.csv')
Run Code Online (Sandbox Code Playgroud)
当然,所有这些只有在安装驱动器后才会起作用。