Extracting pattern from set of files

I was given a directory of problematic files. Each file had a pattern _id_ in it. I needed that id to then remove to a temporary table in T-SQL Here's how:
import os

for root, dirs, files in os.walk("D:\BadFiles"):
    for filename in files:
        file_end = filename[-19:][:6]
        print('INSERT INTO #TEMP(fileid) VALUES ('+str(int(file_end[:6]))+')')

I then created a temporary table in T-SQL and inserted the data :)