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.
- 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:
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) in the last statement is for getting the path to the root (parent directory) of the file.
- os.path.splitext(f_path) is for adding the file extension (if any) of the original file to the timestamp
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Pandas Timestamp.timestamp
- Python script to get device vendor name from MAC Address
- Python | Create an empty text file with current date as its name
- Python script to change MAC address of Linux machine
- MoviePy – Getting Original File Name of Video File Clip
- Python | Print the initials of a name with last name in full
- Python IMDbPY – Getting Person name from searched name
- GUI application to search a country name from a given state or city name using Python
- Convert Python Script to .exe File
- Change Object Display Name using __str__ function - Django Models | Python
- PyQt5 QDateEdit - Name Change Signal
- Program to print its own name as output
- Pafy - Genrating File Name of Stream
- Pafy - Getting File Name of Stream
- Python - Change button color in kivy using .kv file
- Python - Change kivy button size and position using kv file
- Python | Pandas Timestamp.second
- Python | Pandas Timestamp.date
- Python | Pandas Timestamp.ctime
- Get UTC timestamp in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.