Umm...
There are no layers in STL files, stl files contains lots and lots of triangles in 3d space that form a approximation of 3d object.
Slicers then convert STL into gcode that is normally in layers (but doesn’t have to be)
You would have to read in the entire gcode file and count all the Z moves, but even then it not that simple with features like lift on move etc.
If you use something like slicer you can cheat, the comments list the layer height, you can then work from the end of the file and find the highest Z print and then divide by your layer height
This is why most software just shows you a line number of the gcode and the percentage threw the file...