import os
import shutil
import glob


def file_exists(path):
    """
    Checks if a file exists.
    """
    return os.path.exists(path)


def line_count(path):
    """
    Returns number of lines in a file.
    """
    if not os.path.exists(path):
        return 0

    with open(path, "r", encoding="utf-8") as f:
        return len(f.readlines())


def copy_file(src, dst):
    """
    Copies a file from src to dst.
    """
    if not os.path.exists(src):
        raise FileNotFoundError(f"{src} does not exist")

    shutil.copy(src, dst)


def delete_file(path):
    """
    Deletes a file.
    """
    if os.path.exists(path):
        os.remove(path)


def file_size(path, unit="bytes"):
    """
    Returns file size.
    unit: 'bytes' or 'kb'
    """
    if not os.path.exists(path):
        return 0

    size = os.path.getsize(path)

    if unit == "kb":
        return round(size / 1024, 2)

    return size


def list_files(directory, pattern="*.txt"):
    """
    Lists files in a directory matching a pattern.
    """
    search_path = os.path.join(directory, pattern)
    return glob.glob(search_path)
