Python: file management on Windows/Linux that can sort by creation date

Viewed 17

I have to create a system that can manage files that are created by another python script. These are the requirements for the system:

  1. Each file will have a unique name based on its content, but there can be overlaps on similar content (truck, firetruck).
  2. The system should sort the files by creation time (easy on windows with os.get_ctime, but not on linux)
  3. The system should be able to remove files by inputting the unique name

I have come up with something like this, but I assume that there might be an easier way to do it.

def add_file(name):
    creation_time = datetime.now().strftime("%d-%m-%Y-%H-%M-%S")
    filename = f'{creation_time}_{name}'
    numpy.save(filename, data)
def sort_files():
    for root, subfolders, files in os.walk(data_path):
        subfolders.sort()
        for sf in subfolders:
            sf_name = sf.split("_")[1]
def remove_file(name):
    for root, subfolders, files in os.walk(data_path):
        for sf in subfolders:
            if len(sf.split("_")[1].replace(name, "")) == 0:
                os.remove(sf)
              
0 Answers
Related