5

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.

3
  • If you remove the check for the file suffix it should be what you want. Or is there a problem with your code? Commented Aug 21, 2021 at 12:45
  • Put the returned values of os.path.getmtime(file) into a list. When you have assembled a list of all the times, take the max() of the list and convert it to something readable with datetime.datetime.fromtimestamp(). Commented Aug 21, 2021 at 12:54
  • You might find absolute paths useful. Commented Aug 21, 2021 at 12:57

3 Answers 3

4

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)
Sign up to request clarification or add additional context in comments.

3 Comments

The first one worked perfectly! thank you so much! I was able to get the list i was hoping to get: 499169700.0 1624634759.0780153 1629294677.4324608 1629294682.575441
@JacobLoveland My pleasuere!
can you pls help its not working for me its not giving any output
1

Import os module and datetime module. Use path.getmtime() method from os module. It will give you the timestamp. Then convert it to normal time with datetime.fromtimestamp() method from datetime module.

Here is the example:

import os, datetime
n = os.path.getmtime("your file path")
print(datetime.datetime.fromtimestamp(n))

Comments

0

For getmtime the return value is a number giving the number of seconds since the epoch. Hence we are able to use the datetime module to get a datetime object corresponding to that.

from os.path import getmtime
from datetime import datetime as dt

m_time = getmtime('somefile.txt')

time = dt.fromtimestamp(m_time)

Example:

>>> time = getmtime('wsgi.py')
>>> time
1619622258.1773882
>>> dt.fromtimestamp(time)
datetime.datetime(2021, 4, 28, 20, 34, 18, 177388)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.