Python Forum
How to solve this file handling issue?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to solve this file handling issue?
#1
Hi,

I am using the below code to store data on Raspberry Pi using UART whenever it receives any data. Raspberry Pi receives data continuously for 6 seconds. I want to store this data in a single file and open a new one when Raspberry Pi receives data at a later time.

But in my code, whenever Raspberry Pi receives data, a new file is getting created every second and data is being stored in each of those files instead of storing the whole data in the same file. Can someone help/suggest how I can solve the issue? Thanks.
import time
import serial
import datetime
import os

ser = serial.Serial(port='/dev/serial0', baudrate=900000, parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS, rtscts=1,)

while (True):
    if (ser.inWaiting() > 0):
        outputFilePath = os.path.join(os.path.dirname(__file__),
               datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")
        text_file = open(outputFilePath, 'a')
        data_str = ser.read(ser.inWaiting())
        text_file.write(data_str)
        #text_file.flush()
	if ("D" in data_str):        
		text_file.close()
    time.sleep(0.01)
Reply
#2
You could perhaps refactor it like this
import time
import serial
import datetime
import os
 
ser = serial.Serial(port='/dev/serial0', baudrate=900000, parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS, rtscts=1,)
 
def output_file_path():
    return os.path.join(os.path.dirname(__file__),
               datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")
 
while (True):
    if ser.inWaiting():
        with open(output_file_path(), 'w') as text_file:
            while True:
                while not ser.inWaiting():
                    time.sleep(0.01)
                data_str = ser.read(ser.inWaiting())
                text_file.write(data_str)
                if "D" in data_str:
                    break
    time.sleep(0.01)
GiggsB likes this post
Reply
#3
(Jan-10-2022, 07:50 AM)Gribouillis Wrote: You could perhaps refactor it like this

Thank you. It works like a charm. I am a beginner at python, I understood your logic, but I have one doubt. How is this function closing the file, since we are not explicitly closing it? Thanks.
Reply
#4
When the program exits the with open... block, the file is automatically closed.
GiggsB likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 713 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  file handling Newbee question middlecope 2 770 Jan-18-2023, 03:09 PM
Last Post: middlecope
Star python exception handling handling .... with traceback mg24 3 1,265 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Delimiter issue with a CSV file jehoshua 1 1,287 Apr-19-2022, 01:28 AM
Last Post: jehoshua
  File handling issue GiggsB 4 1,419 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How can I solve this file handling issue? GiggsB 17 3,532 Feb-14-2022, 04:37 AM
Last Post: GiggsB
  File handling knollfinder 3 2,044 Jun-28-2020, 07:39 PM
Last Post: knollfinder
  Writing to File Issue Flash_Stang 3 2,514 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  file handling sivareddy 1 1,631 Feb-23-2020, 07:28 PM
Last Post: jefsummers
  Simple Read File Issue blackjesus24 4 2,746 Feb-09-2020, 12:07 AM
Last Post: blackjesus24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020