files = (file for file in os.listdir(path) if os.path.isfile(os.path.join(path, file))) for file in files: # You could shorten this to one line, but it runs on a bit. ...