[Tutor] Write Data to Multiple Files

Mohanad Ismail ismail.mohanad at yahoo.ie
Wed Apr 19 06:35:36 EDT 2017


Hello,
I have recently started using python and i have come to a halt while writing a code and i was hoping someone out there can help me.
I am attempting to store a stream of data directly from a serial port onto multiple files in sequence after x amount of time is elapsed.
for example :
Read data from serial and write to file 1 for 15 sec, after the 15 seconds continue writing the data on to file 2 for 15 seconds and vice versa. in essence each file should write for 15 seconds and stop for 15 seconds while the data is written to the other file
at the moment i have managed to write a code to read and write the data to one file (see below). can someone please assist me.
Code:
#!/usr/bin/env python
from datetime import datetimeimport serial
outfile='/home/bogie/Documents/Data/file1.txt'#outfile='/home/bogie/Documents/Data/file2.txt'ser = serial.Serial(    port='/dev/ttyS4',    baudrate=9600,    parity=serial.PARITY_NONE,    stopbits=serial.STOPBITS_ONE,    bytesize=serial.EIGHTBITS,
)
while ser.isOpen():    d = open(outfile,'a')     dataString = ser.read(44)    d.write(datetime.utcnow().isoformat()+ '\t' + dataString + '\n')    d.flush() #included to force the system to write to disk

ser.close()

Thanks,Mohanad 


More information about the Tutor mailing list