If you prefer an option that does not use Python, you can use Tracert in Windows, or Traceroute in Linux.
Output the code in Tracert
So you could use Python just to run the external command with the help of the subprocess library, instead of trying to use the language's own resources.
The code below executes the command and stores it in a .txt file
# coding: UTF-8
import os
import subprocess
# Se for Windows, apenas troque o traceroute por tracert
command = ['traceroute', 'www.facebook.com']
with open('log.txt', 'w') as arq:
output_command = subprocess.call(command, stdout=arq)
print('[+] Finished')