2
\$\begingroup\$

I have some files such as .\image_1_01.bmp that will be created elsewhere. I am then moving these files to an output directory. If any are missing I inform the user that it's missing and instruct them to check the logs.

Given how similar all the code is, can I make this code shorter?

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)


if retcode != 0:
    sys.exit(1)
else:
    print("Test case is not suitable for your configuration")
    sys.exit(2)

if __name__ == '__main__':
    exit( generic() )`
\$\endgroup\$
0

1 Answer 1

5
\$\begingroup\$

You are right in that most of your code is very similar, except for the file that you are checking. Since only a small part of the code changes, and the same actions are made at every step, this asks for a loop that iterates over the different files that you want to check:

paths_to_check = [".\image_1_01.bmp", ".\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 p in paths_to_check:
    if not os.path.exists(p):
        print(p + 'not generated\n' 'for mor info check the log file' )
        return 1
    else:
        shutil.move(p, output_directory)
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review. Please note, questions that don't have enough context about how the code/output is used are off-topic on this site (and hence why the post was closed). If the OP edits their post to address the problem and make their post on-topic, they make your answer moot. It is advised not to answer such questions. Protip: There are tons of on-topic questions to answer; you'll make more reputation faster if you review code in questions that will get higher view counts for being on-topic. \$\endgroup\$ Commented Mar 10, 2020 at 15:49
  • \$\begingroup\$ Allright, thanks. Should I delete this answer? \$\endgroup\$ Commented Mar 10, 2020 at 16:17
  • 2
    \$\begingroup\$ I'd say leave it for now. The question isn't "crap" like the meta post describes and thus there likely wouldn't be a push for it to be deleted. \$\endgroup\$ Commented Mar 10, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.