mercredi 10 juillet 2019

Function written in Python to list files in a specific folder not filtering out unwanted results

This function is not filtering out files that match the prefix(~?) or extension (eval(not '.xlsm')) nor is it filtering out folders.

All 3 attempts produced the same result. I'm pretty new at this Python stuff so please dumb it down for me what I should do...

ATTEMPT 1

def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    ...
        result = dict([(file, None) for file in os.listdir(folder_path)
                       if os.path.isdir(file) is False and
                       file.startswith(prefix_to_filter_out) is False and
                       file.endswith(extension_to_filter_out) is False])
    ...
    return result

ATTEMPT 2

def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    ...
        result = dict([(file, None) for file in os.listdir(folder_path)
                       if not(os.path.isdir(file)) and
                       not(file.startswith(prefix_to_filter_out)) and
                       not(file.endswith(extension_to_filter_out))])
    ...
    return result

ATTEMPT 3

def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    ...
        result = dict([(file, None) for file in os.listdir(folder_path)
                       if not(os.path.isdir(file))
                       if not(file.startswith(prefix_to_filter_out))
                       if not(file.endswith(extension_to_filter_out))])
    ...
    return result

I expect this to filter out temporary files on Google Drive that start with '~?', filter out folders, and filter out anything besides an Excel workbook.
However, it is including everything- even folders. This is part of an app that pops up a message box anytime a file in a specified folder is added/removed. This gets annoying really quick because it'll pop up every time someone opens or closes a file in Google Drive.

Thanks for the help!

Aucun commentaire:

Enregistrer un commentaire