lundi 14 octobre 2019

How to overcome a ValueError when working with multiple if conditions?

I'm trying to make a script that can identify if a folder name is a project or not. To do this I want to use multiple if conditions. But I struggle with the resulting ValueError that comes from checking, for example, the first letter of a folder name and if it is a Number. If it's a String i want to skip the folder and make it check the next one. Thank you all in advance for your help. Cheers, Benn

I tried While and except ValueError: but haven't been successful with it.

# Correct name to a project "YYMM_ProjectName" = "1908_Sample_Project"

projectnames = ['190511_Waldfee', 'Mountain_Shooting_Test', '1806_Coffe_Prime_Now', '180410_Fotos', '191110', '1901_Rollercoaster_Vision_Ride', 'Musicvideo_LA', '1_Project_Win', '19_Wrong_Project', '1903_di_2', '1907_DL_2', '3401_CAR_Wagon']

# Check conditions

for projectname in projectnames:
    if int(str(projectname[0])) < 3 and int(projectname[1]) > 5 and ((int(projectname[2]) * 10) + int(projectname[3])) <= 12 and str(projectname[4]) == "_" and projectname[5].isupper():
        print('Real Project')
        print('%s is a real Project' % projectname)
        # print("Skipped Folders")

ValueError: invalid literal for int() with base 10: 'E'

Aucun commentaire:

Enregistrer un commentaire