Skip to content

Commit da4968d

Browse files
committed
Created using Colaboratory
1 parent 842e21d commit da4968d

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed

Colab_Special/Snippets_Drive.ipynb

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Snippets: Drive",
7+
"provenance": [],
8+
"collapsed_sections": [],
9+
"toc_visible": true,
10+
"include_colab_link": true
11+
},
12+
"kernelspec": {
13+
"display_name": "Python 3",
14+
"name": "python3"
15+
}
16+
},
17+
"cells": [
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {
21+
"id": "view-in-github",
22+
"colab_type": "text"
23+
},
24+
"source": [
25+
"<a href=\"https://colab.research.google.com/github/gulabpatel/Python_Tutorials/blob/master/Colab_Special/Snippets_Drive.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {
31+
"id": "u22w3BFiOveA"
32+
},
33+
"source": [
34+
"# Mounting Google Drive in your VM\n",
35+
"\n",
36+
"The example below shows how to mount your Google Drive in your virtual machine using an authorization code, and shows a couple of ways to write & read files there. Once executed, observe the new file (`foo.txt`) is visible in https://drive.google.com/\n",
37+
"\n",
38+
"Note this only supports reading and writing files."
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"metadata": {
44+
"id": "7-JR7WQ5O70X"
45+
},
46+
"source": [
47+
"from google.colab import drive\n",
48+
"drive.mount('/gdrive')"
49+
],
50+
"execution_count": null,
51+
"outputs": []
52+
},
53+
{
54+
"cell_type": "code",
55+
"metadata": {
56+
"colab": {
57+
"base_uri": "https://localhost:8080/",
58+
"height": 34
59+
},
60+
"id": "xlUqA3hgfqxf",
61+
"outputId": "88621acf-3778-4a22-8ee4-b02b4e373e4d"
62+
},
63+
"source": [
64+
"with open('/gdrive/My Drive/foo.txt', 'w') as f:\n",
65+
" f.write('Hello Google Drive!')\n",
66+
"!cat '/gdrive/My Drive/foo.txt'"
67+
],
68+
"execution_count": null,
69+
"outputs": [
70+
{
71+
"output_type": "stream",
72+
"text": [
73+
"Hello Google Drive!"
74+
],
75+
"name": "stdout"
76+
}
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {
82+
"id": "bRFyEsdfBxJ9"
83+
},
84+
"source": [
85+
"# Saving data to Google Drive\n",
86+
"\n",
87+
"* [PyDrive reference](https://pythonhosted.org/PyDrive/)\n",
88+
"* [Google Drive API reference](https://developers.google.com/drive/v3/reference/)"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"metadata": {
94+
"id": "to5f2q3wraat"
95+
},
96+
"source": [
97+
"# Import PyDrive and associated libraries.\n",
98+
"# This only needs to be done once in a notebook.\n",
99+
"from pydrive.auth import GoogleAuth\n",
100+
"from pydrive.drive import GoogleDrive\n",
101+
"from google.colab import auth\n",
102+
"from oauth2client.client import GoogleCredentials\n",
103+
"\n",
104+
"# Authenticate and create the PyDrive client.\n",
105+
"# This only needs to be done once in a notebook.\n",
106+
"auth.authenticate_user()\n",
107+
"gauth = GoogleAuth()\n",
108+
"gauth.credentials = GoogleCredentials.get_application_default()\n",
109+
"drive = GoogleDrive(gauth)\n",
110+
"\n",
111+
"# Create & upload a text file.\n",
112+
"uploaded = drive.CreateFile({'title': 'Sample file.txt'})\n",
113+
"uploaded.SetContentString('Sample upload file content')\n",
114+
"uploaded.Upload()\n",
115+
"print('Uploaded file with ID {}'.format(uploaded.get('id')))"
116+
],
117+
"execution_count": null,
118+
"outputs": []
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {
123+
"id": "j5VyISCKFrqU"
124+
},
125+
"source": [
126+
"After executing the cell above, a new file named 'Sample file.txt' will appear in your [drive.google.com](https://drive.google.com/) file list."
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {
132+
"id": "hqJvrn8xsgd0"
133+
},
134+
"source": [
135+
"# Listing files in Google Drive"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"metadata": {
141+
"id": "-f-hfkapsiPc"
142+
},
143+
"source": [
144+
"# Import PyDrive and associated libraries.\n",
145+
"# This only needs to be done once per notebook.\n",
146+
"from pydrive.auth import GoogleAuth\n",
147+
"from pydrive.drive import GoogleDrive\n",
148+
"from google.colab import auth\n",
149+
"from oauth2client.client import GoogleCredentials\n",
150+
"\n",
151+
"# Authenticate and create the PyDrive client.\n",
152+
"# This only needs to be done once per notebook.\n",
153+
"auth.authenticate_user()\n",
154+
"gauth = GoogleAuth()\n",
155+
"gauth.credentials = GoogleCredentials.get_application_default()\n",
156+
"drive = GoogleDrive(gauth)\n",
157+
"\n",
158+
"# List .txt files in the root.\n",
159+
"#\n",
160+
"# Search query reference:\n",
161+
"# https://developers.google.com/drive/v2/web/search-parameters\n",
162+
"listed = drive.ListFile({'q': \"title contains '.txt' and 'root' in parents\"}).GetList()\n",
163+
"for file in listed:\n",
164+
" print('title {}, id {}'.format(file['title'], file['id']))"
165+
],
166+
"execution_count": null,
167+
"outputs": []
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {
172+
"id": "P3KX0Sm0E2sF"
173+
},
174+
"source": [
175+
"# Downloading files or importing data from Google Drive\n",
176+
"\n",
177+
"* [PyDrive reference](https://pythonhosted.org/PyDrive/)\n",
178+
"* [Google Drive API reference](https://developers.google.com/drive/v3/reference/)"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"metadata": {
184+
"id": "TbRbQAYqPTci"
185+
},
186+
"source": [
187+
"# Import PyDrive and associated libraries.\n",
188+
"# This only needs to be done once per notebook.\n",
189+
"from pydrive.auth import GoogleAuth\n",
190+
"from pydrive.drive import GoogleDrive\n",
191+
"from google.colab import auth\n",
192+
"from oauth2client.client import GoogleCredentials\n",
193+
"\n",
194+
"# Authenticate and create the PyDrive client.\n",
195+
"# This only needs to be done once per notebook.\n",
196+
"auth.authenticate_user()\n",
197+
"gauth = GoogleAuth()\n",
198+
"gauth.credentials = GoogleCredentials.get_application_default()\n",
199+
"drive = GoogleDrive(gauth)\n",
200+
"\n",
201+
"# Download a file based on its file ID.\n",
202+
"#\n",
203+
"# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz\n",
204+
"file_id = 'REPLACE_WITH_YOUR_FILE_ID'\n",
205+
"downloaded = drive.CreateFile({'id': file_id})\n",
206+
"print('Downloaded content \"{}\"'.format(downloaded.GetContentString()))"
207+
],
208+
"execution_count": null,
209+
"outputs": []
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"metadata": {
214+
"id": "hauvGV4hV-Mh"
215+
},
216+
"source": [
217+
"# Downloading files to your local file system\n",
218+
"\n",
219+
"`files.download` will invoke a browser download of the file to your local computer.\n"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"metadata": {
225+
"id": "p2E4EKhCWEC5"
226+
},
227+
"source": [
228+
"from google.colab import files\n",
229+
"\n",
230+
"with open('example.txt', 'w') as f:\n",
231+
" f.write('some content')\n",
232+
"\n",
233+
"files.download('example.txt')"
234+
],
235+
"execution_count": null,
236+
"outputs": []
237+
}
238+
]
239+
}

0 commit comments

Comments
 (0)