This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Get last modified date of Folders and Files using os.path module
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gopeshsingh, veky
Priority: normal Keywords:

Created on 2021-08-26 06:34 by gopeshsingh, last changed 2022-04-11 14:59 by admin.

Messages (5)
msg400312 - (view) Author: Gopesh Singh (gopeshsingh) Date: 2021-08-26 06:34
I am trying to get Last modified dates of Folders and Files mounted on Azure Databricks.
I am using following Code:

```
root_dir = "/dbfs/mnt/ADLS1/LANDING/parent"

def get_directories(root_dir):

    for child in Path(root_dir).iterdir():

        if child.is_file():
            print(child, datetime.fromtimestamp(getmtime(child)).date())
      
        else:
            print(child, datetime.fromtimestamp(getmtime(child)).date())
            get_directories(child)
```
The issue is that it is giving wrong dates for some folders.
When I put a wait time of 1 second (time.sleep(.000005)) for each iteration, it gives correct results.
Otherwise, it sometimes give current date or date of folder listed from last iteration.

Seems, when the processing is fast, it is not able to pick correct modified date.

I have explained this issue on other portal: 
https://stackoverflow.com/questions/68917983/get-last-modified-date-of-folders-and-files-in-azure-databricks
msg400313 - (view) Author: Gopesh Singh (gopeshsingh) Date: 2021-08-26 06:42
Adding further details:

//
from os.path import getmtime
from datetime import datetime
from pathlib import Path
import time
//

System:
Operating System: Ubuntu 18.04.5 LTS
Python: 3.8.8
msg400347 - (view) Author: Vedran Čačić (veky) * Date: 2021-08-26 14:30
It probably has nothing to do with your bug, but your title is wrong. You are _not_ getting mtime using pathlib (but using os.path instead). That is done like using this approach: https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat

Just to eliminate irrelevant details, could you try _actually_ implement the algorithm using the above, and see if the results differ?
msg400349 - (view) Author: Gopesh Singh (gopeshsingh) Date: 2021-08-26 14:38
Updated title. As I used getmtime of os.path module.
msg400350 - (view) Author: Gopesh Singh (gopeshsingh) Date: 2021-08-26 14:43
Thanks Vedram for the response. Changed the title.

But I get the same issue with Path.stat().

When I put a sleep time for each iteration, it works fine. Else, it gives current date as modified date.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89172
2021-08-26 14:43:10gopeshsinghsetmessages: + msg400350
2021-08-26 14:38:03gopeshsinghsetmessages: + msg400349
title: Get last modified date of Folders and Files using pathlib module -> Get last modified date of Folders and Files using os.path module
2021-08-26 14:30:21vekysetnosy: + veky
messages: + msg400347
2021-08-26 06:42:21gopeshsinghsetmessages: + msg400313
2021-08-26 06:34:58gopeshsinghcreate