如果在文件夹中找不到映像,程序将崩溃,但它应该记录在日志记录问题中

2024-05-04 06:35:46 发布

您现在位置:Python中文网/ 问答频道 /正文

如果在“图像”文件夹中找不到特定图像,“我的程序”将崩溃。该程序将在“图像”文件夹和csv中的“图像”列中查找图像。如果“图像名称”出现在“图像”列中,但在文件夹中找不到,则会崩溃。我试图记录映像文件,但失败了

这是我到目前为止所做的

import pandas as pd
import os
import shutil                       # making a duplicate copy of a file
import logging
from os.path import splitext        # splits name & extension from a file


class Image:

    def image_fix(self):

        # logging
        LOG = "example.log"
        logging.basicConfig(filename='example.log',
                            filemode='w',
                            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                            datefmt='%H:%M:%S',
                            level=logging.DEBUG)


        # console handler
        console = logging.StreamHandler()
        console.setLevel(logging.ERROR)
        logging.getLogger("").addHandler(console)


        # using panda to open and read csv
        df = pd.read_csv('rgw.csv')
        df['Image'] = df.ImageName + "_" + df.ImageWChain + ".jpg"

        #checking if column "ImageWChain" has "jpg" extension,then concat .jpg
        if ".jpg" not in df.ImageWChain:
            df['ImageWChain'] = df.ImageWChain + ".jpg"

        if ".jpg" not in df.ImageName:
            df['ImageName'] = df.ImageName + ".jpg"

        # write to csv
        df.to_csv('rgw.csv')
        old_image = df.ImageName
        new_image = df.Image

        #splits the imagename and extension
        for item in enumerate(old_image):
            name, ext = splitext(old_image[item[0]])
            if (ext == ""):
                continue
            oldFileName = name + ext
            print("oldFileName = " + oldFileName)
            newFileName = new_image
            print("newFileName = " + newFileName)

            #checks whether image file exits in folder or not
            if (os.path.isfile(oldFileName)):

                #creates duplicate copy of an image
                for old_image, new_image in zip(df.ImageName,df.Image):
                    shutil.copy2(old_image,new_image)

            else:
                # if image not found in folder,then stores in log


                logging.info(oldFileName)

                # write into log
                logger = logging.getLogger(oldFileName)
                logger.debug(" <- This image was not found in the folder")


if __name__=="__main__":
    obj = Image()
    obj.image_fix()

回溯是

C:\Python27\python.exe D:/New/a.py Traceback (most recent call last): 
File "D:/New/a.py", line 23, in <module> shutil.copy2(old_image,new_image)
File "C:\Python27\lib\shutil.py", line 130, in copy2 copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'R0056SS.jpg'

Tags: csvnamein图像imageimportdfif
1条回答
网友
1楼 · 发布于 2024-05-04 06:35:46

此处:

       #checks whether image file exits in folder or not
        if (os.path.isfile(oldFileName)):

您正在测试oldFileName的存在性,但是尝试复制old_image

            #creates duplicate copy of an image
            for old_image, new_image in zip(df.ImageName,df.Image):
                shutil.copy2(old_image,new_image)

相关问题 更多 >