Python Forum
Saving text file with a click: valueerror i/o operation on closed file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving text file with a click: valueerror i/o operation on closed file
#1
Hi guys, first post here. I hope I'm in the right place, since it seems to be more of a Python issue than a PyQt issue.

I've been trying to import the text generated from the console into a text file with a click.

So every time I do it, I'll notify through the console "Beginning measurement". And the data will be streaming into a text file, then I'd like to tell via the console that it's done. Here's the code:

def on_click(self):
            


        clear = lambda: os.system('cls')
        clear()
        print("Beginning measurement...")


        now = datetime.now()
        current_time = now.strftime("%Y%m%d-%H%M%S")

        
        parser = argparse.ArgumentParser(description="Read and print data from MAX30102")
        parser.add_argument("-r", "--raw", action="store_true",
                            help="print raw data instead of calculation result")
        parser.add_argument("-t", "--time", type=int, default=30,
                            help="duration in seconds to read from sensor, default 30")
        args = parser.parse_args()

        with open("/home/pi/Desktop/Output data - " + current_time + self.textbox.text() + ".txt", 'w') as file:
        
                sys.stdout = file
        
                print(self.textbox.text())
                hrm = HeartRateMonitor(print_raw=args.raw, print_result=(not args.raw))
                hrm.start_sensor()
                try:
                    time.sleep(args.time)
                except KeyboardInterrupt:
                    print('keyboard interrupt detected, exiting...')

                hrm.stop_sensor()
                file.close()

        print("Measurement complete.")

        
There are two things which happened:
1. The "measurement complete" printout does not occur
2. I can't restart my measurements unless I re-debug the program, in which the error generated from here is "valueerror: i/o operation on closed file"

I'm a beginner, so anyone who can point me in the right direction here is greatly appreciated.

Thanks!
Vizier87
Reply


Messages In This Thread
Saving text file with a click: valueerror i/o operation on closed file - by vizier87 - Nov-12-2020, 08:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 809 Yesterday, 05:47 AM
Last Post: Bronjer
  very newbie problem on text file zapad 2 218 Apr-12-2024, 06:50 PM
Last Post: zapad
  This result object does not return rows. It has been closed automatically dawid294 6 1,057 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  file open "file not found error" shanoger 8 1,130 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Replace a text/word in docx file using Python Devan 4 3,419 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 772 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 932 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,235 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,109 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,615 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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