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

时间:2016-07-29 09:06:16

标签: python logging

如果在图像文件夹中找不到该特定图像,我的程序会崩溃。程序在图像文件夹中查找图像,在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'

1 个答案:

答案 0 :(得分:1)

这里:

       #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)