samedi 20 novembre 2021

How to simplify long winded IF / Else statement

Faily new new at Python and self taught by googling and short online courses. I am really enjoying Python and want to start cleaning up some of my old code. I'm looking at a way to simplify my long winded, repeated IF/Else statements. Wondering if there is a better way to do it or it is fine what I am doing, maybe a class or something like that.

Here is a sample code of one of the scenarios I am refering too.

for file in os.listdir(src_dir):
    if raceCheck == 'Race_1':
        if fnmatch.fnmatch(file, '*1.pdf'):
            upload_to_aws_site1(os.path.join(src_dir, file), 'official')
            upload_to_aws_site2(os.path.join(src_dir, file))
            print(file)
    elif raceCheck == 'Race_2':
        if fnmatch.fnmatch(file, '*2.pdf'):
            upload_to_aws_site1(os.path.join(src_dir, file), 'official')
            upload_to_aws_site2(os.path.join(src_dir, file))
            print(file)
    elif raceCheck == 'Race_3':
        if fnmatch.fnmatch(file, '*3.pdf'):
            upload_to_aws_site1(os.path.join(src_dir, file), 'official')
            upload_to_aws_site2(os.path.join(src_dir, file))
            print(file)
    elif raceCheck == 'Race_4':
        if fnmatch.fnmatch(file, '*4.pdf'):
            upload_to_aws_site1(os.path.join(src_dir, file), 'official')
            upload_to_aws_site2(os.path.join(src_dir, file))
            print(file)
    else:
        print('I am here: ', file)


Thank you

Aucun commentaire:

Enregistrer un commentaire