如何使用google colaboratory来运行google Drive中的.py文件
1、使用colaboratory创建.ipynb文件
2、授权访问云盘
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
3、挂载google Drive
!mkdir -p drive
!google-drive-ocamlfuse drive
4、指定云盘目录
from google.colab import drive
drive.mount("/content/drive")
5、将路径切换到需要运行的.py文件路径中
import os
path = '/content/drive/My Drive/XXXX'
os.chdir(path)
os.listdir(path)
6、运行代码
!python xxx.py
如果需要使用matlibplot绘图,则使用以下命令
%run xxx.py