问题

运行如下代码

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

会出现如下问题:
AttributeError: module ‘tensorflow’ has no attribute ‘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来处理。