LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add date to python file variable - python 2.6.6 (https://www.linuxquestions.org/questions/linux-newbie-8/add-date-to-python-file-variable-python-2-6-6-a-4175558487/)

MrLinuxDonnelly 11-10-2015 06:07 AM

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 ?

HMW 11-10-2015 06:33 AM

You can use time.

There are probably several ways, but here's how I would do it:
Code:

>>> import time
>>> time.strftime("%Y%m%d")
'20151110'

Best regards,
HMW

MrLinuxDonnelly 11-10-2015 06:52 AM

Quote:

Originally Posted by HMW (Post 5447474)
You can use time.

There are probably several ways, but here's how I would do it:
Code:

>>> import time
>>> time.strftime("%Y%m%d")
'20151110'

Best regards,
HMW

Hi HMW,
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

HMW 11-10-2015 07:02 AM

Quote:

Originally Posted by MrLinuxDonnelly (Post 5447484)
Hi HMW,
thanks for the reply, how do i get this into my filename though ?

Hi!

I would simply build a string, like this:
Code:

>>> CurrDate = time.strftime("Y%m%d")
>>> myPath = "/home/rdonnelly/log/connection-timeout" + CurrDate + ".txt"
>>> myPath
'/home/rdonnelly/log/connection-timeout20151110.txt'

Then you can use it with your open statement like this:
Code:

>>> timeOutFile = open(myPath, 'w')
Best regards,
HMW

MrLinuxDonnelly 11-10-2015 07:10 AM

Quote:

Originally Posted by HMW (Post 5447491)
Hi!

I would simply build a string, like this:
Code:

>>> CurrDate = time.strftime("Y%m%d")
>>> myPath = "/home/rdonnelly/log/connection-timeout" + CurrDate + ".txt"
>>> myPath
'/home/rdonnelly/log/connection-timeout20151110.txt'

Then you can use it with your open statement like this:
Code:

>>> timeOutFile = open(myPath, 'w')
Best regards,
HMW

Perfect got it going now.
Much appreciated.

Code below for reference :

import time
CurrDate = time.strftime("%Y%m%d")
timeoutFile = open("/home/rdonnelly/log/connection-timeout." + CurrDate + ".txt", 'w')

HMW 11-10-2015 07:23 AM

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

MrLinuxDonnelly 11-10-2015 07:25 AM

Quote:

Originally Posted by HMW (Post 5447503)
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

this was simply the distro installed on various servers within my environment.

Ill be upgrading at some point in the future.

Thanks again

HMW 11-10-2015 07:27 AM

Quote:

Originally Posted by MrLinuxDonnelly (Post 5447504)
this was simply the distro installed on various servers within my environment.

Ill be upgrading at some point in the future.

Thanks again

I see. Glad I could help.

Best regards,
HMW


All times are GMT -5. The time now is 12:38 PM.