【问题标题】:OSError: raw write() returned invalid length when using print() in pythonOSError:在 python 中使用 print() 时,原始 write() 返回了无效长度
【发布时间】:2018-05-01 14:31:12
【问题描述】:

我正在使用 python tensorflow 来训练模型以识别 python 中的图像。但是在尝试从github 执行 train.py 时出现以下错误

Traceback (most recent call last):
File "train.py", line 1023, in <module>
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "C:\Users\sande\Anaconda3\envs\tensorflow\lib\site-
packages\tensorflow\python\platform\app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "train.py", line 766, in main
bottleneck_tensor)
File "train.py", line 393, in cache_bottlenecks
jpeg_data_tensor, bottleneck_tensor)
File "train.py", line 341, in get_or_create_bottleneck
bottleneck_tensor)
File "train.py", line 290, in create_bottleneck_file
print('Creating bottleneck at ' + bottleneck_path)
OSError: raw write() returned invalid length 112 (should have been between 0 
and 56)

下面是 create_bottleneck_file() 的代码

def create_bottleneck_file(bottleneck_path, image_lists, label_name, index,
                       image_dir, category, sess, jpeg_data_tensor,
                       bottleneck_tensor):
"""Create a single bottleneck file."""
print('Creating bottleneck at ' + bottleneck_path)
image_path = get_image_path(image_lists, label_name, index,
                          image_dir, category)
if not gfile.Exists(image_path):
    tf.logging.fatal('File does not exist %s', image_path)
image_data = gfile.FastGFile(image_path, 'rb').read()
try:
    bottleneck_values = run_bottleneck_on_image(
        sess, image_data, jpeg_data_tensor, bottleneck_tensor)
except:
    raise RuntimeError('Error during processing file %s' % image_path)

bottleneck_string = ','.join(str(x) for x in bottleneck_values)
with open(bottleneck_path, 'w') as bottleneck_file:
    bottleneck_file.write(bottleneck_string)

我尝试减少文件名,以便bottleneck_path 将是一个小值,但没有奏效。我试图在网上搜索这个错误,但没有找到任何有用的东西。如果您有解决此问题的方法,请告诉我

【问题讨论】:

  • 我认为问题在于您在 Windows 上运行该应用程序,但该代码适用于 Linux 或 Mac OS。如果在第 338 行运行print(bottleneck_path),输出是什么?
  • 我认为你在 cmd 运行时与 cmd 进行了交互,也许这可以帮助stackoverflow.com/questions/48371993/…

标签: python python-3.x image-processing tensorflow


【解决方案1】:

如果您无法像我一样迁移到 3.6 或从 Windows 迁移,请安装 win_unicode_console 包,将其导入并在脚本开头添加此行以启用它:

win_unicode_console.enable()

这个问题似乎是 3.6 之前的 Python 所特有的,因为负责处理文本输出的代码已针对这个最新版本进行了重写。这也意味着我们很可能不会看到针对此问题的修复。

来源:https://bugs.python.org/issue32245

【讨论】:

    【解决方案2】:

    我认为这是 11 月的创作者更新引入的 stdout/stderr 流中的一个错误,它发生在 powershell.exe 和 cmd.exe 中

    这似乎只发生在 Windows 10 版本 1709(操作系统内部版本 16299.64)上。我的猜测是它是 unicode realted(输出大小是预期长度的 两倍

    一个(非常)快速而肮脏的解决方法是只在您的控制台上输出 ASCII:

    mystring.encode("utf-8").decode("ascii")

    https://github.com/Microsoft/vscode/issues/39149#issuecomment-347260954

    【讨论】:

      【解决方案3】:

      在@AMSAantiago 答案中添加更多内容。您可以运行 win_unicode_console.enable()。但不是在每个文件上都使用它,您可以在每次 Python 调用时运行它 (docs)。这对我有用。

      【讨论】:

        猜你喜欢
        • 2016-05-25
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        • 2017-09-05
        • 1970-01-01
        相关资源
        最近更新 更多