Python Learning Tips - Laser Focused on CSV Data Processing

You can split a filename with the .split method filename.split("_"). That’ll give you the DB# and the remainder.

A useful string method is .endswith, and you can split just once on at the end with filename.rsplit(".", 1).

I’d also suggest judicious use of dict and maybe defaultdict to store and categorise the filenames. You can use one to group all the filenames by DB #, for example, and then process all those having the same DB #.