Python script, make it shorter

Viewed 38

How can I make this code shorter? Some code is similar and I think they can be merged into one:

    if not os.path.exists(".\image_1_01.bmp"):
        print('image_1_01.bmp not generated\n' 'for more info check the log file' )
        return 1
    else:
        shutil.move(".\image_1_01.bmp", output_directory)

    if not os.path.exists(".\image_1_01.raw"):
        print('image_1_01.raw not generated\n' 'for mor info check the log file' )
        return 1
    else:
        shutil.move(".\image_1_01.raw", output_directory)

    if not os.path.exists(".\image_1_01_hist.csv"):
        print('image_1_01_hist.csv not generated\n' 'for mor info check the log file' )
        return 1
    else:
        shutil.move(".\image_1_01_hist.csv", output_directory)

    if not os.path.exists(".\image_1_02.bmp"):
        print('image_1_02.bmp not generated\n' 'for more info check the log file' )
        return 1
    else:
        shutil.move(".\image_1_02.bmp", output_directory)

    if not os.path.exists(".\image_1_02.raw"):
        print('image_1_02.raw not generated\n' 'for mor info check the log file' )
        return 1
    else:
        shutil.move(".\image_1_02.raw", output_directory)

    if not os.path.exists(".\image_1_02_hist.csv"):
        print('image_1_02_hist.csv not generated\n' 'for mor info check the log file' )
        return 1
    else:
        shutil.move(".\image_1_02_hist.csv", output_directory)

    if not os.path.exists(".\scan_stats.csv"):
        print('scan_stats.csv not generated\n' 'for mor info check the log file' )
        return 1
    else:
        shutil.move(".\scan_stats.csv", output_directory)`
1 Answers
images = [".\image_1_01.raw",".\image_1_01_hist.csv",".\image_1_02.bmp",".\image_1_02.raw",".\image_1_02_hist.csv",".\scan_stats.csv"]

for image in images:
    if not os.path.exists(image):
        print('{} not generated\n' 'for mor info check the log file'.format(image) )
        return 1
    else:
        shutil.move(image, output_directory)
Related