通过Google Colab将文件保存到Google驱动器时出错

时间:2019-12-04 15:08:57

标签: python pandas google-drive-api google-colaboratory

我正在尝试从colab笔记本中将文件保存到我的Google云端硬盘中,但我仍然遇到相同的错误。我已经挂载了驱动器。当我打电话给pwd时,我知道了,这似乎是正确的:

/content/drive/My Drive/

这是示例代码和读出的内容:

from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print(df)
df.to_csv('test.csv')

         A   B   C   D
    0   38  28  18  74
    1   36  54  84  13
    2    2   1  55  42
    3   69  20  15  40
    4   83  58  81  76
    ..  ..  ..  ..  ..
    95  92  65   3  50
    96  25  15  82  84
    97  60  24  29  10
    98  13  26  94  25
    99  46  36  91  24

    [100 rows x 4 columns]
    ---------------------------------------------------------------------------
    OSError                                   Traceback (most recent call last)
    <ipython-input-10-d5c5784f87b6> in <module>()
          4 df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
          5 print(df)
    ----> 6 df.to_csv('test.csv')

    2 frames
    /usr/local/lib/python3.6/dist-packages/pandas/io/common.py in _get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text)
        397         if encoding:
        398             # Encoding
    --> 399             f = open(path_or_buf, mode, encoding=encoding, newline="")
        400         elif is_text:
        401             # No explicit encoding

    OSError: [Errno 30] Read-only file system: 'test.csv'





1 个答案:

答案 0 :(得分:2)

要保存到Google驱动器,应添加安装路径:

from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print(df)
df.to_csv('/content/drive/My Drive/test.csv')