If you're writing python on a computer where you've connected the device via serial or USB, it's pretty easy. There's a serial library that does the hard work of reading serial.
Something like
import serial
port='/dav/ttyACM0'
ser = serial.Serial(port,115200,timeout=1)
line = ser.readline()
print (line.decode('utf8'))
#open your text file and write the line, might want to decode the binary
If you send the USBSD command, you can access the SD card just like any device/file and put your file there.
If you want to put this on the device itself, perhaps someone else can give you some hints. I haven't tried that.