Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code output to a file and add timestamp to filename

"I would not even approach this from a python-fix perspective, but simply redirect the output of running your python script: python /path/to/script/myscript.py > /path/to/output/myfile.txt Nothing has to change in your script, and all print statements will end up in your text file."

how can i use the code above to output to a file, but also timestamp the filename? example: python /path/to/script/myscript.py > /path/to/output/myfile01-22-2014.txt

like image 495
user3255477 Avatar asked Jan 31 '14 00:01

user3255477


1 Answers

I did something like this to add the timestamp to my file name.

import time, os, fnmatch, shutil


t = time.localtime()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
BACKUP_NAME = ("backup-" + timestamp)
like image 60
Eli Avatar answered Oct 03 '22 15:10

Eli