Ad

Python Rename Relative Path With Current Timestamp

- 1 answer

How to check, in Python, if some relative path exist and rename it to current timestamp. Example: path name is /home/folder-20190228/ after starting script i want to check if this path exist and rename it to current timestamp /home/folder-20190301/

Ad

Answer

You can check whether a directory exists with os.path.isdir and you will get the current timestamp with the datetime library.

import datetime
import os

path = "/home/folder-20190228/"
current_timestamp = f"{datetime.datetime.now():%Y%m%d}"

if os.path.isdir(path):
    path = '-'.join([path.split('-')[0], current_timestamp])
Ad
source: stackoverflow.com
Ad