def write_xyz(crystal, fname, comment=None):
    """
    Generate an atomic coordinates .xyz file from a crystal structure.

    Parameters
    ----------
    crystal : crystals.Crystal
        Crystal to be converted.
    fname : path-like
        The XYZ file will be written to this file. If the file already exists,
        it will be overwritten.
    comment : str or None, optional
        Comment to include at the second line of ``fname``.
    """
    comment = comment or ''
    with open(fname, 'wt', encoding='ascii') as file:
        file.write(str(len(crystal)) + '\n')
        file.write(comment + '\n')
        for atom in crystal.itersorted():
            x, y, z = atom.coords_cartesian
            row = (
                f'  {atom.element:<2}       {x:10.5f}       {y:10.5f}       {z:10.5f}'
                )
            file.write(row + '\n')
