Search code examples
pythonmacosif-statementkmlsimplekml

How to save a file on macos desktop using kml in python?


I have just read some of posts on how to save a kml file on macos desktop in Python but i couldn't find the answer.I try to locate the file path but it doesn't work. my code is:

import pathlib
import simplekml

longitude = input("Enter longitude: ")
latitude = input("Enter longitude: ")

kml = simplekml.Kml()
kml.newpoint(name="Sample", coords=[(longitude, latitude)])
kml.save("~/Desktop/bura/Points.kml")

The problem starts with kml.save("~/Desktop/bura/Points.kml") it doesn't find the path and doesn't save the file.


Solution

  • kml probably does not expand ~, but you can do it explicitly:

    import os
    ...
    kml.save(os.path.expanduser("~/Desktop/bura/Points.kml"))