X
X
xenu-xore2020-12-04 13:49:33
Python
xenu-xore, 2020-12-04 13:49:33

How to create files with date in title?

Hello! I'm just learning, please help me solve this problem.

Task:
1) It is necessary to create a list of files of the same type with a name - 'N' and content 'X'
2) When re-creating files, check if there is a file with the name N , if so, rename it to this form - 'old_N_23-12-02' the content should remain 'X'
3) So the file named N should always be the latest one with content 'Y'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-12-04
@xenu-xore

You can use string or f-string formatting to create the desired name.
strftime for the desired date format.

import datetime

N = "work"

filename = "old_{}_{}.txt".format(N, datetime.date.today().strftime("%d-%m-%y"))
print(filename)

To check the existence of a file os.path.exists(path_to_file)
To rename os.rename(from where to)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question