问题
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'
回答1:
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')
来源:https://stackoverflow.com/questions/59179049/error-saving-files-into-google-drive-via-google-colab