Simple function for reading external molecule files

There was an excellent post on : How to read multiple external geometry files? Improve documentation

Currently, I use a function like the one below, to read geometry external files. I was searching for a more simple method in the psi4.core classes that might implement something similar and more generalized to read many other common formats.

def read_mol_file(fname, format_str):
    """
    Reads an external xyz file from working directory
    into a string and returns a molecule_string that 
    can used with psi4.gemometry()
    --------
    fname : Must be of the form 'filename.format' or 'fname.txt'
    format_str: supports formats: ['xyz+', , ] 
    """
    file_str = open(fname, 'r').read() 
    mol_str = qcdb.Molecule.from_string(file_str, 
                                dtype=format_str)
    mol_psi4_str = mol_str.create_psi4_string_from_molecule()
    return mol_psi4_str

Have you tried passing the string into geometry directly?

Also, you forgot to close the file object.

Oh! yes, just the string works for sure, but are the other external file formats supported by psi4.geometry(mol_str) too? From what I saw in psi4numpy tutorials and psi4 docs, I think xyz and zmatrix are supported. Will it work for .pdb , .sdf etc too?

I don’t expect it to, but try it yourself and find out.