program crashes if image is not found in the folder,but it should record in log- logging issue

36

Here :

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

You are testing the existence of oldFileName, but then trying to copy 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)
Share:
36

Related videos on Youtube

sqllover999
Author by

sqllover999

Updated on November 30, 2022

Comments

  • sqllover999
    sqllover999 over 1 year

    my program crashes if that particular image is not found in the image folder.The program looks for image in the image folder and imagecolumn in the csv.If the imagename is present in the imagecolumn but not found in the folder ,then it crashes. i tried to log the imagefile,but failed.

    This is I have so far

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

    The traceback is

    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'
    
    • Julien
      Julien over 7 years
      What do you mean by crash? Do you get an error message? If so please share it.
    • sqllover999
      sqllover999 over 7 years
      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'
    • bruno desthuilliers
      bruno desthuilliers over 7 years
      How is this problem related to logging ? Do you expect the logger to automagically catch exceptions and log them ???
    • bruno desthuilliers
      bruno desthuilliers over 7 years
      As a side note: the way you use the loggin module is just a complete mess...
  • sqllover999
    sqllover999 over 7 years
    i am trying to check if the image exists in the folder and csv column if not then write in log