조회 수 47 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
tip_colab_파일저장및업로드_colab

Open In Colab

PC, 구글 드라이브에서 파일 업로드/다운로드

PC에서 파일을 선택하여 업로드 하는 방법

files.upload returns a dictionary of the files which were uploaded. The dictionary is keyed by the file name, the value is the data which was uploaded.

In [0]:
from google.colab import files

uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))
Upload widget is only available when the cell has been executed in the current browser session. Please rerun this cell to enable.
Saving e-7-신경망-케라스.ipynb to e-7-신경망-케라스.ipynb
User uploaded file "e-7-신경망-케라스.ipynb" with length 66021 bytes
In [0]:
!ls
e-7-신경망-케라스.ipynb  haarcascade_frontalface.xml  sample_data

파일을 만들고 PC로 다운로드하는 방법

In [0]:
import pandas as pd
df = pd.DataFrame([1,2,3])
df
Out[0]:
0
0 1
1 2
2 3
In [0]:
df.to_csv('new_file.txt')
!ls
e-7-신경망-케라스.ipynb  haarcascade_frontalface.xml  new_file.txt  sample_data
In [0]:
files.download('new_file.txt')

구글 드라이브에서 파일을 읽는 방법

인증번호를 입력하여 현재 가상 머신을 자신의 구글 드라이브와 연결시킬 수 있다.

In [0]:
from google.colab import drive
drive.mount('/gdrive')
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

Enter your authorization code:
··········
Mounted at /gdrive
In [0]:
!ls /gdrive/My\ Drive/Colab\ Notebooks/data
accidents.txt  car_accidents.xls  new_file_accidents.txt
In [0]:
accidents = pd.read_csv('/gdrive/My Drive/Colab Notebooks/data/accidents.txt', delimiter='\t')
accidents
Out[0]:
기간 지역 발생건수 자동차 1만대당 발생건수 사망자수 인구 10만명당 사망자수 부상자수 인구 10만명당 부상자수
0 2017 합계 38,625 108.3 343 3.4 53,810 531.5
1 2017 종로구 1,190 185.3 13 7.9 1,689 1,028.3
2 2017 중구 1,252 188.7 4 3.0 1,684 1,251.2
3 2017 용산구 1,337 142.9 12 4.9 1,949 797.3
4 2017 성동구 1,076 86.9 15 4.8 1,501 480
5 2017 광진구 1,064 90.9 16 4.3 1,441 387.1
6 2017 동대문구 1,718 141.2 25 6.8 2,139 584.4
7 2017 중랑구 1,639 118.6 6 1.5 2,228 539.8
8 2017 성북구 1,557 108.5 17 3.7 2,068 454.1
9 2017 강북구 1,202 120.5 9 2.7 1,567 477.7
10 2017 도봉구 944 84.3 9 2.6 1,239 357.9
11 2017 노원구 1,499 88.3 11 2.0 2,055 368.2
12 2017 은평구 1,067 71.2 12 2.4 1,489 303.1
13 2017 서대문구 1,032 102.2 8 2.5 1,430 440
14 2017 마포구 1,859 138.9 8 2.1 2,793 724
15 2017 양천구 1,343 79.3 13 2.7 1,789 376.6
16 2017 강서구 1,584 71.8 12 2.0 2,155 354.3
17 2017 구로구 1,433 89.4 12 2.7 1,960 443.9
18 2017 금천구 824 84.0 6 2.4 1,228 484.4
19 2017 영등포구 2,386 146.7 22 5.5 3,519 875.3
20 2017 동작구 1,409 117.5 20 4.9 2,043 500.1
21 2017 관악구 1,251 85.3 11 2.1 1,659 318.5
22 2017 서초구 2,361 120.7 21 4.7 3,548 796.6
23 2017 강남구 3,469 136.0 19 3.4 4,959 883.9
24 2017 송파구 2,758 109.9 23 3.4 3,805 566.9
25 2017 강동구 1,371 89.6 19 4.3 1,873 425.3

메뉴창 이용하기

  • 좌측의 메뉴창 '파일'에서 마운트 되어 있는 구글 드라이브의 내용을 확인할 수 있다.
  • PC에서 직접 업로드/다운로드 할 수 있다.

구글 드라이브에 파일을 보내는 방법

In [0]:
!ls /gdrive/My\ Drive/Colab\ Notebooks/data
accidents.txt  car_accidents.xls  new_file_accidents.txt
In [0]:
accidents.to_csv('/gdrive/My Drive/Colab Notebooks/data/new_file_accidents.txt')
!ls /gdrive/My\ Drive/Colab\ Notebooks/data

아래는 PyDrive, 구글 시트, 구글 클라우드를 이용하는 방법

PyDrive

The example below shows 1) authentication, 2) file upload, and 3) file download. More examples are available in the PyDrive documentation

In [0]:
!pip install -U -q PyDrive

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# PyDrive reference:
# https://gsuitedevs.github.io/PyDrive/docs/build/html/index.html

# 2. Create & upload a file text file.
uploaded = drive.CreateFile({'title': 'Sample upload.txt'})
uploaded.SetContentString('Sample upload file content')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

# 3. Load a file by ID and print its contents.
downloaded = drive.CreateFile({'id': uploaded.get('id')})
print('Downloaded content "{}"'.format(downloaded.GetContentString()))
Uploaded file with ID 14vDAdqp7BSCQnoougmgylBexIr2AQx2T
Downloaded content "Sample upload file content"
In [0]:
with open('/gdrive/foo.txt', 'w') as f:
  f.write('Hello Google Drive!')
!cat /gdrive/foo.txt
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-19-9c12c6cc4374> in <module>()
----> 1 with open('/gdrive/foo.txt', 'w') as f:
      2   f.write('Hello Google Drive!')
      3 get_ipython().system('cat /gdrive/foo.txt')

OSError: [Errno 95] Operation not supported: '/gdrive/foo.txt'

Drive REST API

The first step is to authenticate.

In [0]:
from google.colab import auth
auth.authenticate_user()

Now we can construct a Drive API client.

In [0]:
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')

With the client created, we can use any of the functions in the Google Drive API reference. Examples follow.

Creating a new Drive file with data from Python

In [0]:
# Create a local file to upload.
with open('/tmp/to_upload.txt', 'w') as f:
  f.write('my sample file')

print('/tmp/to_upload.txt contains:')
!cat /tmp/to_upload.txt
/tmp/to_upload.txt contains:
my sample file
In [0]:
# Upload the file to Drive. See:
#
# https://developers.google.com/drive/v3/reference/files/create
# https://developers.google.com/drive/v3/web/manage-uploads
from googleapiclient.http import MediaFileUpload

file_metadata = {
  'name': 'Sample file',
  'mimeType': 'text/plain'
}
media = MediaFileUpload('/tmp/to_upload.txt', 
                        mimetype='text/plain',
                        resumable=True)
created = drive_service.files().create(body=file_metadata,
                                       media_body=media,
                                       fields='id').execute()
print('File ID: {}'.format(created.get('id')))
File ID: 1Cw9CqiyU6zbXFD9ViPZu_3yX-sYF4W17

After executing the cell above, a new file named 'Sample file' will appear in your drive.google.com file list. Your file ID will differ since you will have created a new, distinct file from the example above.

Downloading data from a Drive file into Python

In [0]:
# Download the file we just uploaded.
#
# Replace the assignment below with your file ID
# to download a different file.
#
# A file ID looks like: 1uBtlaggVyWshwcyP6kEI-y_W3P8D26sz
file_id = 'target_file_id'

import io
from googleapiclient.http import MediaIoBaseDownload

request = drive_service.files().get_media(fileId=file_id)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
  # _ is a placeholder for a progress object that we ignore.
  # (Our file is small, so we skip reporting progress.)
  _, done = downloader.next_chunk()

downloaded.seek(0)
print('Downloaded file contents are: {}'.format(downloaded.read()))
Downloaded file contents are: my sample file

Google Sheets

Our examples below will use the existing open-source gspread library for interacting with Sheets.

First, we'll install the package using pip.

In [0]:
!pip install --upgrade -q gspread

Next, we'll import the library, authenticate, and create the interface to sheets.

In [0]:
from google.colab import auth
auth.authenticate_user()

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())

Below is a small set of gspread examples. Additional examples are shown on the gspread Github page.

Creating a new sheet with data from Python

In [0]:
sh = gc.create('A new spreadsheet')

After executing the cell above, a new spreadsheet will be shown in your sheets list on sheets.google.com.

In [0]:
# Open our new sheet and add some data.
worksheet = gc.open('A new spreadsheet').sheet1

cell_list = worksheet.range('A1:C2')

import random
for cell in cell_list:
  cell.value = random.randint(1, 10)

worksheet.update_cells(cell_list)

After executing the cell above, the sheet will be populated with random numbers in the assigned range.

Downloading data from a sheet into Python as a Pandas DataFrame

We'll read back to the data that we inserted above and convert the result into a Pandas DataFrame.

(The data you observe will differ since the contents of each cell is a random number.)

In [0]:
# Open our new sheet and read some data.
worksheet = gc.open('A new spreadsheet').sheet1

# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)

# Convert to a DataFrame and render.
import pandas as pd
pd.DataFrame.from_records(rows)
[['10', '5', '6'], ['9', '6', '2']]

Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
» Python Google Colab에서 파일 업로드/다운로드 팁 2019.03.06 47 0
458 Python pandas, matplot 자주사용하는 코드 2019.03.06 37 0
457 Python matplot에서 한글이 보이도록 하는 코드 2019.03.06 41 0
456 일반 OMV (OpenMediaVault) 플러그인들 2019.02.10 215 0
455 일반 ddns 정보 secret 2019.02.10 0 0
454 Android git archive 를 사용해서 폴더를 .git 제외하고 tar 나 zip 으로 묶기 1 2019.02.10 205 0
453 LINUX vi 설정하기(.vimrc) 2019.02.01 164 0
452 Pi 칩 저항 사이즈표, 사이즈변환, 와트, 오차표 2019.01.11 413 0
451 Pi NPN과 PNP 트랜지스터의 차이 2019.01.03 456 0
450 Pi 포토커플러 Photo Coupler 특징, 종류, 출력, 구조, 동작 원리, 파라미터 2019.01.03 581 0
449 LINUX vim 에디터 이쁘게 사용하기 2018.08.06 1595 0
448 Pi 아두이노에 도움이 되는 전자부품들 2018.06.20 799 0
447 일반 Visual Studio Code 환경 설정 및 사용 방법 1 2018.06.01 6521 0
446 Pi 라즈베리파이 음성인식 S/W 2018.04.26 1221 0
445 Pi 파이썬 코드를 이용한 파이카메라 제어 2018.04.24 1411 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 31 Next ›
/ 31

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5