0% found this document useful (0 votes)
64 views

Python Script To Change Name of A File To Its Timestamp - GeeksforGeeks

Uploaded by

Dixon Craig
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Python Script To Change Name of A File To Its Timestamp - GeeksforGeeks

Uploaded by

Dixon Craig
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python Competitive Programming Machine Learning

Python Script to change name of a file to its timestamp


Last Updated : 29 Dec, 2020

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:

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and Got It !
Python3 understood our Cookie Policy & Privacy Policy
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 quantifers)
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
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
output of strftime(). This format specifier could be changed in order to the syntax of other
understood our Cookie Policy & Privacy Policy
timestamp
Got It !
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

Like 0

Previous Next

Calculate the QR decomposition of a given How to determine Period Range with


matrix using NumPy Frequency in Pandas?

RECOMMENDED ARTICLES Page : 1 2 3

01 Python | Pandas Timestamp.timestamp


21, Jan 19 05 How to add timestamp to CSV file in Python
23, Aug 21

02 Python | Create an empty text file with current date


as its name 06 How to add timestamp to excel file in Python
11, Sep 21
01, Aug 19

03 How to save file with file name from user using


Python?
07 MoviePy – Getting Original File Name of Video File
Clip
22, Jul 20
13, Jan 21

04 Python Program to Get the File Name From the File


Path 08 Python | Print the initials of a name with last name
in full
26, Feb 22 10, Dec 17

Article Contributed By :
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and Got It !
understood our Cookie Policy & Privacy Policy
VasuDev4
@VasuDev4
Vote for di�culty

Easy Normal Medium Hard Expert

Article Tags : Python os-module-programs, python-os-module, python-utility, Python

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and Got It !
understood our Cookie Policy & Privacy Policy
5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305

[email protected]

Company Learn

About Us Algorithms

Careers Data Structures

In Media SDE Cheat Sheet

Contact Us Machine learning

Privacy Policy CS Subjects

Copyright Policy Video Tutorials

News Languages

Python
Top News
Java
Technology
CPP
Work & Career
Golang
Business
C#
Finance
SQL
Lifestyle

Web Development Contribute

Web Tutorials Write an Article

Django Tutorial Improve an Article

HTML Pick Topics to Write

CSS Write Interview Experience

JavaScript Internships

Bootstrap Video Internship

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and Got It !
understood our Cookie Policy & Privacy Policy

You might also like