Add date to python file variable - python 2.6.6
So i have my python script doing its thing, however i want to now output its output to a dated file.
Currently i have timeoutfile = open('/home/rdonnelly/log/connection-timeout.txt', 'w') however i want it to be in the format of connection-timeout.YYYYMMDD.txt Any suggestions on the module etc ? |
You can use time.
There are probably several ways, but here's how I would do it: Code:
>>> import time HMW |
Quote:
thanks for the reply, how do i get this into my filename though ? timeoutfile = open('/home/rdonnelly/log/connection-timeout(".txt', 'w') I know for bash id simply put the varibale like this /home/rdonnelly/log/connection-timeout`date +%Y%m%d`.txt but getting into python has seemed to baffle me |
Quote:
I would simply build a string, like this: Code:
>>> CurrDate = time.strftime("Y%m%d") Code:
>>> timeOutFile = open(myPath, 'w') HMW |
Quote:
Much appreciated. Code below for reference : import time CurrDate = time.strftime("%Y%m%d") timeoutFile = open("/home/rdonnelly/log/connection-timeout." + CurrDate + ".txt", 'w') |
Glad you got it sorted out. Just out of curiosity, how come you opt for Python 2.x instead of Python 3?
Best regards, HMW |
Quote:
Ill be upgrading at some point in the future. Thanks again |
Quote:
Best regards, HMW |
All times are GMT -5. The time now is 12:38 PM. |