【问题标题】:Loop over image filenames and split them循环图像文件名并拆分它们
【发布时间】:2018-08-04 17:26:30
【问题描述】:

我正在尝试从目录中切出一堆图像。尝试遍历它们,但我得到FileNotFoundError: [Errno 2] No such file or directory: '45678.png'

实际上,这是其中一个文件的名称。这是我的代码:

import image_slicer
import os

indir = '/Users/data/h3'
for root, dirs, filenames in os.walk(indir):
    for file in filenames:
        if file.endswith('.png'):
            image_slicer.slice(file, 4)

目录没问题,我不明白为什么找不到文件,当根据错误消息实际找到它时

【问题讨论】:

  • 你应该在这个问题中添加 [python] 标签。

标签: python loops split filenames


【解决方案1】:

使用slice()打开文件名时需要添加路径。

试试image_slicer.slice(os.path.join(dirpath, name), 4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    相关资源
    最近更新 更多