Search code examples
pythondirectorylast-modifieddatemodified

How do I pull the 'last modified time' of each file within a directory in Python?


I have been tasked with creating a small application that allows the user to: browse and choose a specific folder that will contain the files to be checked daily, browse and choose a specific folder that will receive the copied files, and manually initiate the 'file check' process that is performed by the script. In order to do that, I need to get the last modified time and add an if statement that checks if the file is over 24hrs old, then it will move the file over to the correct folder. I think I know how to do all that. Where I'm running into issues is with the os.path.getmtime method. As far as i understand it, I cannot get the modified time of multiple files. I have to name the file specifically. So the '../File Transfer/Old Files/' doesnt work. I keep getting the same error:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'LICENSE-MIT.txt' 

(that's the first txt file in the Old Files folder) if I change the code to:

x = os.path.getmtime(../File Transfer/Old Files/LICENSE-MIT.txt)

It works, but with one complication. It gives me the time in seconds which is in the 449 billion range. I'm able to convert that number to a usable number by using this code:

y = int(x)
z = y//60//60
print(z)

This keeps it as an int and not a float, and converts it from seconds to hours. Then I would just do a >=24 if statement to see if it's older than a day. HOWEVER, this is still just one the single file basis, which really doesnt work for what i want the program to do. I want it to do this with all files in a folder, regardless of what the names of those files are, or how many of them there are. ANY help would be super appreciated!

Here's the script I'm using to iterate the folder and get each file mod time individually:

for file in os.listdir('../File Transfer/Old Files/'):
    if file.endswith ('.txt'):
        time_mod = os.path.getmtime(file)
        print(time_mod)

Ignore the last line. That was just for me so see the results for troubleshooting.


Solution

  • The os.listdir() method lists the files of the given path excluding the path, hence you will need to concatenate the path yourself:

    for file in os.listdir('../File Transfer/Old Files/'):
        if file.endswith('.txt'):
            time_mod = os.path.getmtime('../File Transfer/Old Files/' + file)
            print(time_mod)
    

    The glob.glob() method works great in cases like this:

    import os
    import glob
    
    for file in glob.globr('../File Transfer/Old Files/*.txt'):
        time_mod = os.path.getmtime('../File Transfer/Old Files/' + file)
        print(time_mod)
    

    You can get the amount of hours passed since the last modification of each file like so:

    import os
    from time import time
    
    PATH = '../File Transfer/Old Files/'
    
    for file in os.listdir(PATH):
        if file.endswith('.txt'):
            time_mod = time() - os.path.getmtime(PATH + file)
            print(time_mod // 3600)