LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 06:07 AM   #1
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

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, 06:33 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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, 06:52 AM   #3
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

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, 07:02 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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, 07:10 AM   #5
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

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, 07:23 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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, 07:25 AM   #7
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

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, 07:27 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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



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 06:32 AM
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 09: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 08:11 PM
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 12:35 PM
Python: Can you add text to an image in python? BrianK Programming 2 04-03-2008 03:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:08 AM.

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
Open Source Consulting | Domain Registration