0
0
0ralo2019-09-30 15:41:11
Python
0ralo, 2019-09-30 15:41:11

How to find the right file?

In general, I have a database that is constantly automatically saved

while main_thread.is_alive():
        sleep(7200)
        with open(path, 'r') as f:
            data = json.loads(f.read())
            i = now.strftime("%d-%m-%Y-%H-%M")
        with open(f"Saves/{i}.json", 'w') as f:
            json.dump(data, f)
        sleep(36000)

I need to make a method that will pull out the latest safe from the folder and update the database with a copy of this save. How can I pull the last save, preferably without having to worry about finding the nearest date in the file name. I would like by the position in the folder or the date of creation, thanks !

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2019-09-30
@0ralo

Isn't it easier when creating a save just to write the name of the save file in a specific file?
And in Python - just read the name of this file.

T
tsarevfs, 2019-09-30
@tsarevfs

Depending on the traversal method, the traversal order is either undefined or specified as alphabetical by name.
Get a list of files and find the maximum by date in it.
https://docs.python.org/3/library/glob.html
https://stackoverflow.com/a/49871409/1762922
By the way, getting the date from the file name is also not difficult. strptime with the same format string will do most of the work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question