8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CloudMLからgoogle cloud storageのファイルにアクセスする

Last updated at Posted at 2016-10-21

CloudML上でpython標準のopen()関数を使ってgoogle cloud storageのURI(gs://...)にアクセスしようとするとエラーを吐く。

スタックオーバーフローで調べてみると、次のようなエントリを見つけた。
http://stackoverflow.com/questions/40133223/pickled-scipy-sparse-matrix-as-input-data
CloudML上からgoogle cloud storage上のファイルにアクセスする際には、tensorflowのfile_ioパッケージを使う必要があるらしい。
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/lib/io/file_io.py

file_io_test
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from tensorflow.python.lib.io import file_io

path = "./local_file.txt"
#ファイルポインタを取得
#pathはローカルのファイル、cloud storage上のファイルどちらのも対応している
fp = file_io.FileIO(path,"w")
fp.write("HelloWorld\n")
fp.close()

#with構文にも対応
with file_io.FileIO(path,"w") as fp:
    fp.write("HelloWorld\n")

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?