Getting corrupted output files from my program python/sql

Not sure if its an issue with an independent function or an issue with data extraction in general- `

def read_file(file_path):
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"File not found: {file_path}")
    with open(file_path, 'r') as file:
        return file.readlines()


# Parse Baselight data
def parse_baselight(data):
    parsed_frames = []
    for line in data:
        if "<err>" in line:
            continue
        components = line.strip().split()
        if len(components) < 2:
            continue
        filename = components[0]
        frame_data = components[1:]
        numeric_frames = sorted(set(validate_numeric(frame) for frame in frame_data if validate_numeric(frame)))
        if numeric_frames:
            frames = format_frames(numeric_frames)
            parsed_frames.append((filename, frames))
    return parsed_frames


def validate_numeric(value):
    try:
        return int(value)
    except ValueError:
        return None


def format_frames(frames):
    ranges = []
    start = frames[0]
    end = frames[0]
    for i in range(1, len(frames)):
        if frames[i] == end + 1:
            end = frames[i]
        else:
            ranges.append(f"{start}-{end}" if start != end else f"{start}")
            start = frames[i]
            end = frames[i]
    ranges.append(f"{start}-{end}" if start != end else f"{start}")
    return ", ".join(ranges)


def parse_xytech(data):
    parsed_orders = []
    for line in data:
        if '/' in line:
            components = line.strip().split('/')
            location = components[1].strip()
            workorder = components[-1].strip()
            parsed_orders.append((location, workorder))
    return parsed_orders


def populate_database(baselight_data, xytech_data):
    conn = sqlite3.connect("thecrucible_databasedb")
    cursor = conn.cursor()

    cursor.execute("""
        CREATE TABLE IF NOT EXISTS Baselight (
            Location TEXT,
            Frames TEXT
        )
    """)

    cursor.execute("""
        CREATE TABLE IF NOT EXISTS Xytech (
            Location TEXT,
            Workorder TEXT
        )
    """)

    cursor.executemany("INSERT INTO Baselight (Location, Frames) VALUES (?, ?)", baselight_data)
    cursor.executemany("INSERT INTO Xytech (Location, Workorder) VALUES (?, ?)", xytech_data)

    conn.commit()
    conn.close()


def get_video_length(video_path):
    result = subprocess.run(
        [
            "ffprobe",
            "-i", video_path,
            "-v", "error",
            "-show_entries", "format=duration",
            "-of", "default=noprint_wrappers=1:nokey=1"
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    try:
        return float(result.stdout.strip())
    except ValueError:
        print("Error extracting video length.")
        return 0


def find_matching_ranges(video_length, frame_ranges):
    matching_ranges = []
    fps = 24
    video_frames = int(video_length * fps)
    for frame_range in frame_ranges:
        if '-' in frame_range:
            start, end = map(int, frame_range.split('-'))
        else:
            start = end = int(frame_range)
        if start <= video_frames and end <= video_frames:
            matching_ranges.append(frame_range)
    return matching_ranges


def export_to_xls(data, output_path):
    wb = Workbook()
    ws = wb.active
    ws.title = "Matched Data"
    ws.append(["Filename", "Frame Range"])
    for row in data:
        ws.append(row)
    wb.save(output_path)


def export_unused_to_csv(unused_data, output_path):
    with open(output_path, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["Filename", "Frame Range"])
        writer.writerows(unused_data)
`




`

I am writing a Python script to process metadata from two text files (Baselight and Xytech) and a video file. The script performs the following operations:

Reads and parses metadata from Baselight and Xytech text files.
Populates an SQLite database with this metadata.
Processes the video to find matching frame ranges.
Exports matched data to an Excel file (output.xls) and unused data to a CSV file (unused.csv).
The script works without any crashes, but all output files (output.xls, unused.csv, and thecrucible_databasedb) are corrupted or unusable:

The database file cannot be opened in a SQLite viewer.
The Excel file (output.xls) shows errors when opened in Excel.
The CSV file contains garbled or invalid data.
Steps I’ve Taken:
Debugged the parsed data and confirmed it is in the correct format.
Added error handling for database inserts and file writing operations.
Verified permissions for writing to the output directory.
Cleared previous corrupted files before running the script again.
Tested with minimal, valid input files to isolate the issue

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật