Create text file

Thread Solved

Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Create text file

 
0
  #1
Jul 8th, 2009
Hi All,

I am using the below code to write into a text file

  1. # Write into log file
  2. inp=file(Doc\Log.txt, 'w')
  3. inp.write('Log file start')
  4. inp.close()

The text file 'Log.txt' is present in 'Doc' folder inside the application folder.
Now I want to create a text file with a time stamp every time the User runs the application and then write contents into it. How do I do this?

Any help is much appreciated!...

Regards,
Dinil
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Create text file

 
0
  #2
Jul 8th, 2009
Here's an example of how to get the current date and time (as a string) using the datetime module.
  1. import datetime
  2. timestamp = str(datetime.datetime.now())
  3. """result ->
  4. '2009-07-08 01:16:25.968000'
  5. """
Hope that helps!
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Create text file

 
0
  #3
Jul 8th, 2009
Thanks... i used the below code and its working perfectly..
Thanks a lot

  1. lt = time.localtime(time.time())
  2. lt=[lt[1], lt[2], lt[0], lt[3], lt[4],lt[5]]
  3. lt=str(lt[0])+"-"+str(lt[1])+"-"+str(lt[2])+"-"+str(lt[3])+"hr"+str(lt[4])+"min"+str(lt[5])+"s"
  4.  
  5. strFile = "Log"+"_"+str(lt)+".txt"
  6. file = open(strFile , "a")
  7. file.write('Log file starts')
  8. file.close()

Thanks once again!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: baki100 is an unknown quantity at this point 
Solved Threads: 10
baki100 baki100 is offline Offline
Light Poster

Re: Create text file

 
0
  #4
Jul 8th, 2009
you can also try this it would constitute for a lot less coding
  1. import time
  2. #here you can format the date string anyway you want
  3. time_stamp = time.strftime('%Y-%m-%d %H:%M:%S', (time.localtime(time.time())))
  4. #output for time_stamp -> '2009-07-08 14:22:41'
  5. #and format the text file name like such
  6. strFile = "Log_%s.txt"%time_stamp
Last edited by baki100; Jul 8th, 2009 at 9:22 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Create text file

 
0
  #5
Jul 9th, 2009
oh yes.. that makes it even more simpler.. Thanks a lot again....
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: baki100 is an unknown quantity at this point 
Solved Threads: 10
baki100 baki100 is offline Offline
Light Poster

Re: Create text file

 
0
  #6
Jul 9th, 2009
if you're using windows then having file names with a colon( in it is not allowed and you'll get an exception
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC