How to create a .phy file for constructing a phylogenetic tree?
You simply use Biopython's SeqIO and AlignIO.
The documentation is https://biopython.org/wiki/AlignIO 
etc ...
The format you seek is phylip