python
import os
import shutil
import datetime
now = datetime.datetime.now()
file_name = 'test.txt'
#test.If you have a file called txt
#Create a backup file that includes the date and time in the file name
if os.path.exists(file_name):
shutil.copy(file_name, '{}.{}'.format(
file_name, now.strftime('%Y_%m_%d_%H_%M_%S')))
#test.Create a file with the name txt and the contents are test
with open(file_name, 'w') as f:
f.write('test')
Recommended Posts