How to create a file name with the current date time in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating a file name with the current date and time is a common Python pattern for logs, exports, backups, and generated reports. The usual solution is to get the current time with datetime.now() and format it with strftime() into a string that is readable, sortable, and safe for use in filenames.
The Basic Pattern
A straightforward example looks like this:
Typical output:
This works well because the name is easy to read, sorts naturally by time, and avoids awkward punctuation such as colons.
Why strftime() Is the Right Tool
strftime() lets you control the exact shape of the timestamp. Common format codes include:
- '
%Yfor the four-digit year' - '
%mfor the month' - '
%dfor the day' - '
%Hfor the hour' - '
%Mfor the minute' - '
%Sfor the second'
For a more compact file name, you can remove the dashes:
This produces names such as:
The nice property of this layout is that lexical sort order also matches chronological order.
Avoid Unsafe Filename Characters
A common beginner mistake is to format the timestamp the way it would look on screen and then reuse it directly as a filename:
That prints fine, but the colon characters are a poor choice in actual filenames on some operating systems. For filenames, separators such as _ and - are much safer, which is why %H-%M-%S is usually better than %H:%M:%S.
Create the File Directly
Here is a complete example that generates the name and writes a file using pathlib:
This is a clean pattern for scripts that produce logs, snapshots, or output reports.
Add More Precision When Needed
If the code can create more than one file in the same second, second-level precision may not be enough. Add microseconds with %f:
That makes collisions much less likely in fast loops or concurrent tasks.
Use UTC for Cross-System Consistency
If filenames are generated on different machines or in different time zones, UTC is often more predictable than local time:
This makes the timezone explicit and avoids later confusion about what the timestamp really means.
A Reusable Helper Function
If timestamped filenames appear often in your project, wrap the pattern in a helper:
This keeps naming consistent across the codebase.
Combine with a Directory Path
It is common to create the timestamped name inside an output directory:
This pattern is especially useful for scheduled jobs and data exports.
Common Pitfalls
The biggest pitfall is using characters such as : in the filename because they look natural in timestamps. They are fine for display, but not a good universal choice for filesystem names.
Another pitfall is assuming second-level precision is always enough. If your program writes files very quickly, add %f for microseconds.
A third pitfall is using local time when the files need to be compared across systems in different regions. In those cases, UTC is often clearer.
Finally, avoid overengineering the format. A filename that is readable and sorts well is usually better than a clever but cryptic naming scheme.
Summary
- Use
datetime.now().strftime(...)to generate timestamped filenames in Python - Prefer filename-safe separators such as
_and- - Add microseconds if same-second collisions are possible
- Use UTC when timestamps must stay consistent across systems
- Wrap the pattern in a helper when your project creates timestamped files regularly

