简体   繁体   中英

Latest file path is not returned using glob in python

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;
我的文件夹结构

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM