vendredi 6 janvier 2023

What is the best principle to write files to a folder and delete them after a while within one script in Python?

I query the sql server and then write the result into a separated csv file in a folder every 10 secs. Then I want to delete those files which are older than 3 days and let the script run forever. So What I did is:

def write_csv():
    # connect to sql server and query the info
    # write the query result to csv files and store it in folder A

def delete_old_csv():
    # check the date of the files within a loop
    # delete files if they are older than 3 days
    
while True:
    write_csv()
    delete_old_csv()
    time.sleep(10)

I think it's not a good design that the delete_old_csv() to be called every 10 secs looping through files date. Since this is more of a IO bound task, is multi threading a good approach to design here? If yes, how to call these functions into threads? If my question is wrong here please guide me where to ask this? Thank you for any help.

Aucun commentaire:

Enregistrer un commentaire