执行shell命令:
语句前加 ' ! '可以执行shell命令
!ls
输出:
create_data.py example.txt save train.py
datalab file_list.txt show 牛津花卉数据集
基本上大部分 linux命令都可以这样执行,但是好像vim命令不可以
基本代码段的使用:
google colab提供了一下方法操作云端的文件:
从个人电脑上上传文件到云端:
点击直接运行就可以了调用浏览器功能进行上传了。
将文件从云端下载到个人电脑上:
点击直接运行就可以 调用浏览器自带的下载功能下载(但是有时候Firefox浏览器没有响应,Chrome浏览器是正常的,不知道为什么)
从google云硬盘上传送文件到云端:
将上面的bucket_name改为自己driver的名字,object参数改为想要上传的文件名,open函数中路径改为自己想要上传到的路径。
好像不能传送文件夹,只能传送文件
import自己的py文件:
举例:
你要import一个 example.py的程序,你就另开一个cell写:
%%writefile example.py
代码代码代码
再另开一个cell写:
execfile('example.py')
然后就可以正常import你要的程序了。
参考:
https://stackoverflow.com/questions/47345004/in-googles-colab-notebook-how-do-i-call-a-function-from-a-python-file
以google drive中的指定文件夹作为工作目录:
!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}
!mkdir -p drive
!google-drive-ocamlfuse drive
import os
os.chdir("drive/你的文件夹/")
最后两个功能没有试过直接从其他博客复制过来的(原文:https://blog.csdn.net/haruko666/article/details/79617210),另外还有很多 google提供的api没有提到,有兴趣可以自己去探索。