Display file and directory timestamp details

Display file creation date and modification date using Python.

Written by rakesh.parija

Last published at: May 19th, 2022

In this article we show you how to display detailed timestamps, including the date and time when a file was created or modified.

Use ls command

The simplest way to display file timestamps is to use the ls -lt <path> command in a bash shell.

For example, this sample command displays basic timestamps for files and directories in the /dbfs/ folder.

%sh

ls -lt /dbfs/

Output:

total 36
drwxrwxrwx 2 root root 4096 Jul  1 12:49 FileStore
drwxrwxrwx 2 root root 4096 Jul  1 12:49 databricks
drwxrwxrwx 2 root root 4096 Jul  1 12:49 databricks-datasets
drwxrwxrwx 2 root root 4096 Jul  1 12:49 databricks-results
drwxrwxrwx 2 root root 4096 Jul  1 12:49 ml
drwxrwxrwx 2 root root 4096 Jul  1 12:49 tmp
drwxrwxrwx 2 root root 4096 Jul  1 12:49 user
drwxrwxrwx 2 root root 4096 Jun  9  2020 dbfs
drwxrwxrwx 2 root root 4096 May 20  2020 local_disk0

Use Python commands to display creation date and modification date

The ls command is an easy way to display basic information. If you want more detailed timestamps, you should use Python API calls.

For example, this sample code uses datetime functions to display the creation date and modified date of all listed files and directories in the /dbfs/ folder. Replace /dbfs/ with the full path to the files you want to display.

%python

import os
from datetime import datetime
path = '/dbfs/'
fdpaths = [path+"/"+fd for fd in os.listdir(path)]
print(" file_path " + " create_date " + " modified_date ")
for fdpath in fdpaths:
  statinfo = os.stat(fdpath)
  create_date = datetime.fromtimestamp(statinfo.st_ctime)
  modified_date = datetime.fromtimestamp(statinfo.st_mtime)
  print(fdpath, create_date, modified_date)

Output:

 file_path  create_date  modified_date
/dbfs//FileStore 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730
/dbfs//databricks 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730
/dbfs//databricks-datasets 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730
/dbfs//databricks-results 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730
/dbfs//dbfs 2020-06-09 21:11:24 2020-06-09 21:11:24
/dbfs//local_disk0 2020-05-20 22:32:05 2020-05-20 22:32:05
/dbfs//ml 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730
/dbfs//tmp 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730
/dbfs//user 2021-07-01 12:49:45.264730 2021-07-01 12:49:45.264730