Python Forum
Problem with file not saving current output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with file not saving current output
#1
I have an issue with writing to a file in a for loop. I am appending all of the logged characters to a list. The list has an active listener attached to it, so is actively appending.

When I go to write this list to a file, it works great. However, I am having a problem with closing the file and saving the information. Once the file closes, the information is lost and the file only saves with the newest character of the list after the cutoff limit.

So basically, I need a way to save the file before I close it, so it will write all of the contents to a file.
The "logs" list is being updated by the on_press(key) function, and it's continiually appenidng characters to the Logs list.
When the file writes, it writes fine. When it closes, it closes and loses all of the information that was in the logs list, hence I get an empty TXT file.


def on_press(key):
    logging.info(str(key))
    logs.append(key)
    with open('your_log.txt', 'w') as f:
        if len(logs) < 10:
            for item in logs:
                f.write('%s\n' % item)
        else:
            if len(logs) > 10:
                for item in logs:
                    f.write('%s\n' % item)   #will write the length of the log list which is 9 characters
                    if len(logs) > 10:     
                        break
                    f.close()
#if the length of the log list goes above 10, then the file closes
#and only writes what has been updated to the list from a previous logging function, but does not write prior
#info
Reply
#2
Admittedly I just glanced your code quickly, but I suspect your placing of f.close() might be problematic. It is called only if the else clause is executed and only if break (on line 13) does not occur. Look into that.

You want to consider using context manager to work with files. It will close the file automatically after the code (inside the context manager) is finished processing. You can search online, or this forums, since the topic was brought up many times in the past.

By the way, what happens if len(logs) is 10)?
Reply
#3
I have messed around with the if/else quite a bit. I'll have to revisit.

The 10 is just a test number - I'm trying to achieve closing the file when the
Lost reached a certain number of characters.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 228 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  problem in output of a snippet code akbarza 2 367 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 688 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,195 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,102 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 927 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Saving the times a script is run to a file or ... 3Pinter 7 1,396 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 817 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Saving the print result in a text file Calli 8 1,794 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 858 Sep-09-2022, 05:44 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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