python slice a string and use .find to get last three letters to determine the type of file

what i am asking is how can i correct my if statement, essentially i have a file attachment called ‘fileName’ and i am trying to get the last 3 letters from that file to determine if that type of file is in my config (csv, txt).

valid_filename = myconfig file (csv, txt)

def load_file():
    try:
        # get file from read email and assign a directory path (attachments/file)
        for fileName in os.listdir(config['files']['folder_path']):
            # validate the file extension per config
            # if valid_filename:  else send email failure
            valid_filename = config['valid_files']['valid']
            if fileName[-3:].find(valid_filename):
                file_path = os.path.join(config['files']['folder_path'], fileName)
                # open file path and read it as csv file using csv.reader
                with open(file_path, "r") as csv_file:
                    csvReader = csv.reader(csv_file, delimiter=',')
                    first_row = True

let me know if i can clarify anything better

>Solution :

find() method returns -1 if the string you’re searching for is not found. To check if the element exists in the string, check if find returns -1.

Leave a Reply