How do I get file creation and modification date/times?
There are a few ways to get the file creation and modification date/times, depending on the programming language and operating system you are using. Here are a few examples:
In Python, you can use the os.path
module to get the file's creation and modification date/times:
import os
file_path = '/path/to/file.txt'
# Get file modification time
mod_time = os.path.getmtime(file_path)
# Get file creation time (only works on Windows)
try:
create_time = os.path.getctime(file_path)
except OSError:
create_time = None
Watch a video course
Python - The Practical Guide
In Java, you can use the java.io.File
class to get the file's last modified date:
import java.io.File;
File file = new File("/path/to/file.txt");
long lastModified = file.lastModified();
In bash, you can use the stat
command to get the file's creation and modification date/times:
# Get file modification time
mod_time=$(stat -f "%m" /path/to/file.txt)
# Get file creation time (only works on macOS and Linux)
create_time=$(stat -f "%B" /path/to/file.txt)
Please note that file creation time is not available on all operating systems, and the above examples may not work on all OS.