Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading