3

I am trying to keep a watch on the log folder. If any new file is created then the file path must get returned. for that I have used the following code:

import glob
list_of_files_in_real_predicted = glob.iglob(r'logging\real_predicted\*')
latest_file_in_real_predicted = max(list_of_files_in_real_predicted, key=os.path.getctime)
print(latest_file_in_real_predicted)

The output returned is: logging\real_predicted\log935.csv
instead of: logging\real_predicted\log0.csv

Here is the snapshot of the folder and one can see the latest file created;
my folder structure

Please let me know what I can do to get the latest created file.

2
  • 3
    what is showed in the screenshot is the Date modified, so you have to use os.path.getmtime to get this value. Commented Nov 30, 2018 at 13:21
  • 2
    I'd suggest to add the output of os.stat('log0.csv') and os.stat('log935.csv), since os.path.getctime uses os.stat underneath. Commented Nov 30, 2018 at 13:22

1 Answer 1

3

getctime is different from getmtime. What you're seeing (and the one really useful & widely used) in windows is modification time. You want:

latest_file_in_real_predicted = max(list_of_files_in_real_predicted, key=os.path.getmtime)

Modification time matches the last modification of the contents of the file. Probably why everyone uses it.

getctime isn't even the file creation date:

The ctime indicates the last time the inode was altered

source: Difference between python - getmtime() and getctime() in unix system

Sign up to request clarification or add additional context in comments.

2 Comments

Ok let me check it out. May be this is what I was missing.
it just cannot be otherwise.

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.