473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 16754
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

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

Similar topics

2
by: Randy Jackson | last post by:
Hello all. Okay, this seems really stupid, but it's driving me up the wall. I have a simple script I've written to log some information to a text file. Everything seems to be okay, the code...
0
by: Kamus of Kadizhar | last post by:
Thanks to Robert Brewer, I got enough insight into logging to make it work.... Now I have another issue: file locking. Sorry if this is a very basic question, but I can't find a handy reference...
9
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get...
24
by: Xah Lee | last post by:
suppose i'm calling two system processes, one to unzip, and one to “tail” to get the last line. How can i determine when the first process is done? Example: subprocess.Popen(); ...
15
by: Sullivan WxPyQtKinter | last post by:
I have a huge log file which contains 3,453,299,000 lines with different lengths. It is not possible to calculate the absolute position of the beginning of the one billionth line. Are there...
7
by: ammmmmu | last post by:
Hi all, I am using VB 5.0 and msaccess as a database, I am reading the data from logfiles and inserting it in db, its not throwing any error, but after excecution I not find any records in table...
4
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... ..
4
by: mvvdsteen | last post by:
Hello all, I'm quite new to c++. I made a small program that will help me analyse wind tunnel data. But now I want this program to write to a text file. This works just fine, except it discards...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.