'How to sort files by time and also rename them?

I want to get all files (.txt) in a directory, sorted by time and then get renamed like name1,name2,... using python

Here's what I tried:

import os
def rename (directory):
    os.chdir(directory)  
    result = sorted (filter (os.path.isfile, os.listdir('.')), key-os.path.getmtime)
        ('\n'.join(map(str, result)))
        os.rename (name, "0"+name)

path=input("Enter the file path")
rename(path)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source