469,270 Members | 1,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

how to insert a new line at the end of a file (logfile)

Hi there!

I wonder how to add a new line at the end of my already existing file?

/flaerpen
Jul 6 '07 #1
7 16284
elbin
27
Hi there!

I wonder how to add a new line at the end of my already existing file?

/flaerpen
Expand|Select|Wrap|Line Numbers
  1. >>> f=open('c:\\log.txt', 'r+')   ## open the file in reading and appending mode
  2. >>> len(f.readlines())    ## initial check of number of lines
  3. 3
  4. >>> f.seek(2)   ## go to the end of the file, not necessary here because we already read all of it, but in general...
  5. >>> f.write('\n')   ## write a newline character
  6. >>> f.seek(0)   ##  go to the beginning and check lines again...
  7. >>> len(f.readlines())
  8. 4
  9. >>> f.close()
  10.  
Hope this is what you need. It is important to know where your pointer is in the file, or you can make a mess. Thus the seek function is useful.
Jul 6 '07 #2
bvdet
2,851 Expert Mod 2GB
You also could open the file in append mode:
Expand|Select|Wrap|Line Numbers
  1. >>> f = open('your_file', 'a')
  2. >>> f.write('\n')
  3. >>> f.close()
Jul 6 '07 #3
elbin
27
You also could open the file in append mode:
Expand|Select|Wrap|Line Numbers
  1. >>> f = open('your_file', 'a')
  2. >>> f.write('\n')
  3. >>> f.close()
Well, yes, but to count the lines in the example I needed the 'r'. There may be another way, but I don't know it.
Jul 6 '07 #4
bartonc
6,596 Expert 4TB
Well, yes, but to count the lines in the example I needed the 'r'. There may be another way, but I don't know it.
bvdet has given the preferred answer. The simpler the better according to The Zen of Python.
Jul 6 '07 #5
ghostdog74
511 Expert 256MB
Well, yes, but to count the lines in the example I needed the 'r'. There may be another way, but I don't know it.
Expand|Select|Wrap|Line Numbers
  1. open("file","a").write("\n")
  2.  
if you really need to count lines, use len(open(....).readlines()) first , before you do the appending.
Jul 7 '07 #6
elbin
27
bvdet has given the preferred answer. The simpler the better according to The Zen of Python.
Point taken. I just wanted to make it understandable, although I maybe forget that most people here know python better than me .
Jul 8 '07 #7
bartonc
6,596 Expert 4TB
Point taken. I just wanted to make it understandable, although I maybe forget that most people here know python better than me .
I hope you noticed that I said "preferred" instead of "right". The right answer is always what works best for the individual. But the Zen thing is a great guide.
Jul 8 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Randy Jackson | last post: by
reply views Thread by Kamus of Kadizhar | last post: by
24 posts views Thread by Xah Lee | last post: by
15 posts views Thread by Sullivan WxPyQtKinter | last post: by
5 posts views Thread by amit.uttam | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.