简体   繁体   中英

Error saving files into google drive via google colab

I am trying to save files onto my Google Drive from a colab notebook and I keep getting the same error. I have already mounted my drive. When I call pwd, I get, which seems right:

/content/drive/My Drive/

Here is an example code and read-out:

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'





You want to save to google drive, you should add the mounted path:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM