形成文件路径时的错误

时间:2018-10-16 14:00:07

标签: python python-3.x

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文件夹中,而不是完整路径中。为什么会发生这种情况?

2 个答案:

答案 0 :(得分:1)

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

import os

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

Python os library reference

答案 1 :(得分:0)

您正在构建相对路径,并且相对路径是根据current working directory而不是针对脚本所在的目录来解析的。如果要强制将文件存储在给定目录中,则必须使用绝对路径。

此外,请勿通过字符串连接构建路径,而应使用os.path模块。