[PYTHON] datetime and file write and backup

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

datetime and file write and backup
Read and write csv file
Read and write a file
Write and read a file
Write O_SYNC file in C and Python
[Python3] Read and write with datetime isoformat with json
Restore iPhone backup file
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
Device and Linux file system
Conversion between unixtime and datetime
About Python datetime and timezone
Linux file and directory permissions
[Python] Write to csv file with Python
Switch argparse file specification and pipeline
NAS backup with php and rsync
Write standard output to a file
Python CSV file reading and writing
[Linux] File and directory operation commands
Python memo ① Folder and file operations