LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2015, 07:07 AM   #1
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Rep: Reputation: Disabled
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 ?
 
Old 11-10-2015, 07:33 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
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
 
Old 11-10-2015, 07:52 AM   #3
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
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
 
Old 11-10-2015, 08:02 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
Quote:
Originally Posted by MrLinuxDonnelly View Post
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
 
Old 11-10-2015, 08:10 AM   #5
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
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')
 
Old 11-10-2015, 08:23 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
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
 
Old 11-10-2015, 08:25 AM   #7
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
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
 
Old 11-10-2015, 08:27 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
Quote:
Originally Posted by MrLinuxDonnelly View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rocket/Core/Python/Python.h: No such file or directory shogun1234 Linux - Games 3 10-12-2014 07:32 AM
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 10:15 AM
LXer: Scripting KVM with Python, Part 2: Add a GUI to manage KVM with libvirt and Python LXer Syndicated Linux News 0 01-17-2012 09:11 PM
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 01:35 PM
Python: Can you add text to an image in python? BrianK Programming 2 04-03-2008 04:56 PM


All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration