Search code examples
pythontimestampfile-comparison

Comparing files using timestamp in Python


I'm trying to compare my files with the timestamp.
For example, there is a directory with files, like
a.xls, a.log, a.txt
b.xls, b.log, b.txt
c.xls, c.log, c.txt
here i have to find whether the timestamp(datetime) of a.log and a.txt is greater than timestamp of a.xls
I did research about how to get a timestamp of a file, comparing timestamp of two files.
I don't know whether I'm going in a correct direction.
Please direct me how to look into this problem and give some logical guidance.


Solution

  • I have managed to write a code for the above question.

    import os
    path = 'D:/Newfolder/A'
    xls_file = []
    for i in os.listdir(path):
        if i.endswith('xls'):   
            file_name = (i.split('.')[0])  
            ext_name = (i.split('.')[1])   
            xls_file.append(file_name)   
    for fname in os.listdir(path):  
        for i in xls_file: 
            if fname.__contains__(i) and not fname.endswith('xls'):
                log_txt = os.path.getmtime(path+'/'+fname)   
                xls_tym = os.path.getmtime(path+'/'+i+'.xls')  
                if xls_tym < log_txt:
                    pass
                else:
                    print(i+'.xls')
    

    It may not be the clean code,but i think the code works fine
    Please suggest if any changes had to be made