Python Script to change name of a file to its timestamp Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report A Digital Timestamp is a sequence of characters(usually a combination of digits and delimiters), identifying the time when a certain event occurred. In computer science, timestamps are generally used for marking the time of the creation of a virtual entity but are not limited to this in its use case. Digital Timestamps are implemented in various standards, each favoring a particular use case. i.e. some standards make use of less precise timestamps (only storing date of event), some standards encode the timezone information in the timestamp. But the base syntax of timestamp remains largely the same between standards, which prevents alienation and provides flexibility in choosing one. In this article, we will learn how to obtain the date of creation of a file and would use that to create an ISO 8601 timestamp. Which would be used to name the file. Functions Used: os.path.getctime(): os.path.getctime() method in Python is used to get system’s ctime of the specified path. Here ctime refers to the last metadata change for specified path in UNIX while in Windows, it refers to path creation time.time.strptime(): It is used to convert the string object to time object.time.strftime(): time.strftime(format[, t]) function convert a tuprl or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument.If t is not provided, the current time as returned by localtime() is used. The format must be a string.os.rename(): os.rename() method in Python is used to rename a file or directory.This method renames a source file/ directory to specified destination file/directory. File before Rename: Python3 import time import os # Getting the path of the file f_path = "/location/to/gfg.png" # Obtaining the creation time (in seconds) # of the file/folder (datatype=int) t = os.path.getctime(f_path) # Converting the time to an epoch string # (the output timestamp string would # be recognizable by strptime() without # format quantifiers) t_str = time.ctime(t) # Converting the string to a time object t_obj = time.strptime(t_str) # Transforming the time object to a timestamp # of ISO 8601 format form_t = time.strftime("%Y-%m-%d %H:%M:%S", t_obj) # Since colon is an invalid character for a # Windows file name Replacing colon with a # similar looking symbol found in unicode # Modified Letter Colon " " (U+A789) form_t = form_t.replace(":", "꞉") # Renaming the filename to its timestamp os.rename( f_path, os.path.split(f_path)[0] + '/' + form_t + os.path.splitext(f_path)[1]) File after Rename: Things to keep in mind while using the above code: This code is for Windows OS. For Operating systems other than windows, the users can omit the form_t = form_t.replace(":", "꞉") statement, as it is only required in windows as the OS doesn't allow a colon as a filename. For use in other OS, the last statement (os.rename()) should also be modified accordingly.The argument to strftime(), "%Y-%m-%d %H:%M:%S" is a format specifier. This is used to guide the output of strftime(). This format specifier could be changed in order to the syntax of other timestamp standards.os.path.split(f_path)[0] in the last statement is for getting the path to the root (parent directory) of the file.os.path.splitext(f_path)[1] is for adding the file extension (if any) of the original file to the timestamp Create Quiz Comment V vasudev4 Follow 1 Improve V vasudev4 Follow 1 Improve Article Tags : Python python-utility python-os-module Python os-module-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like