質問:AttributeError:モジュール 'tensorflow'には属性 'gfile'がありません



Question Attributeerror



目次

問題

次のコードを実行します

if not tf.gfile.exists(DATA_DIRECTORY): tf.gfile.makedirs(DATA_DIRECTORY) with tf.gfile.GFile(filepath) as f:

次の問題が発生します。
AttributeError:モジュール「tensorflow」に属性「gfile」がありません



原因と解決策

問題の原因:現在のバージョンでは、gfileはioパッケージのfile_io.pyで定義されています。

解決策1

したがって、次のように変更するだけです。



if not tf.io.gfile.exists(DATA_DIRECTORY): tf.io.gfile.makedirs(DATA_DIRECTORY) with tf.io.gfile.GFile(filepath) as f:

解決策2

上記のコードは、実際には、指定されたパスの下にフォルダーが存在するかどうかを判別するためのものです。存在しない場合は、フォルダーを作成します。
したがって、Pythonのos.pathで処理できます。