943,580 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4713
  • Python RSS
Jul 8th, 2009
0

Create text file

Expand Post »
Hi All,

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

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008
Jul 8th, 2009
0

Re: Create text file

Here's an example of how to get the current date and time (as a string) using the datetime module.
python Syntax (Toggle Plain Text)
  1. import datetime
  2. timestamp = str(datetime.datetime.now())
  3. """result ->
  4. '2009-07-08 01:16:25.968000'
  5. """
Hope that helps!
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 8th, 2009
0

Re: Create text file

Thanks... i used the below code and its working perfectly..
Thanks a lot

Python Syntax (Toggle Plain Text)
  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!
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008
Jul 8th, 2009
0

Re: Create text file

you can also try this it would constitute for a lot less coding
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
baki100 is offline Offline
79 posts
since Apr 2009
Jul 9th, 2009
0

Re: Create text file

oh yes.. that makes it even more simpler.. Thanks a lot again....
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008
Jul 9th, 2009
0

Re: Create text file

if you're using windows then having file names with a colon( in it is not allowed and you'll get an exception
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
baki100 is offline Offline
79 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: binary strings
Next Thread in Python Forum Timeline: Run user given python scripts





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC