简体   繁体   中英

Exit nested function with Python

I am writing a Python script to read a text file, that is updated every hour, parce some data from it and then write to a csv file. I can parce the data and write to the csv no problem. I am trying to implement the ability to make a new csv file for each day, so that the files stay small enough for a human to read to and allow for easy searching on past data.I also want to name that csv with the date. Again That part works. My script reads the time stamp of the text file and parses it into a string. I also have a parsed time string saved to a text file that way I can compare. The problem I have is that even if the date is the same and the time is the same my script will still write the same data to the csv instead of exiting the script until next time it is run under cron. The line that I believe is the problem is line 16. I feel that it is the problem because when it compares the two strings it should find that they are the same and the script should exit. However, it does not do this and continues to write the script anyway. This may not be the problem and the real problem may be some where else, but I am not experienced enough to find the flaw. I have been researching this problem for over a week and tried searching every possible problem I could think of and none have resolved the issue. Any help would be much appreciated. I think I have everything commented well enough for peer review, let me know if I need to explain anything better.

def date_check(new_date, save_date): # checks if date is the same
    if new_date != save_date: # if dates are not the same the call new_csv to make new csv
        new_csv(new_date) # pass date string of new file for file name  
    if new_date == save_date: # if the dates are the same then call time check      
        time_check(new_time, save_time) # pass variables to compare

def time_check(new_time, save_time): # function to check if time is the same    
    if new_time != save_time: # if times are not same then parce the data
        parcer(time_string, data) # pass time_string and data string to be appended 
    if new_time == save_time: # if times are the same then(same file) exit program
        sys.exit() # exit program completely, THIS PART DOES NOT WORK

Since this is the #1 search result in google for "exit nested function python", the answer to the question in the title is:

Use sys.exit() eg

import sys

def foo():
    print("Loading bar")
    while(True):
        bar()

def bar():
    sys.exit()

def main():
    print("Loading foo")
    while(True):
        foo()

if __name__ == '__main__':
    main()

I'm presuming that the OP solved their question since this was opened a year ago, if anyone is stuck with something similar it looks like new_time is never equal to save_time and so sys.exit() never gets evaluated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM