首页 > 解决方案 > 形成文件路径时的错误

问题描述

start() 函数连续运行,直到被强制停止。

import time
import datetime
import math
import image_processing

images_folder = "images/"
folder_name = images_folder + str(datetime.datetime.now().date())
screen_data = image_processing.getScreenData()
stack_collection = image_processing.getStackImages()

def start():
    for item in screen_data:
        image_name = str(math.floor(time.time()))
        image_path = folder_name + "/" + str(item['screen_area']) + "/" + image_name + ".png"
        image_processing.imaging(item['x_coordinate'], item['y_coordinate'], item['width'], item['height'],
                                         image_path, item['screen_area'])

问题是 - 有时(不经常)一些图像保存在/images文件夹中,而不是完整路径。为什么会发生这种情况?

标签: pythonpython-3.x

解决方案


要构建目录路径,您始终可以使用该os库:

import os

os.path.join(folder_name , str(item['screen_area']) , image_name + ".png")

Python 操作系统库参考


推荐阅读