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.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 3
    what is showed in the screenshot is the `Date modified`, so you have to use `os.path.getmtime` to get this value. – Ghilas BELHADJ Nov 30 '18 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. – mehdix Nov 30 '18 at 13:22

1 Answers1

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219