Solved

How to use file.write in python without new line

Posted on 2011-09-29
4
531 Views
Last Modified: 2012-06-21
Hi,

If I do a for loop in python of this list,
file = open("foo.txt",'w')
list = ["hi", "how", "are", "you"]
for element in list:
    file.write(element)
it will write a file like this
hi
how
are
you

I.e., put a new line after each element...

What if I don't want a new line after each element except for the last element in the list? is that possible?

I.e., I want:
hi how are you \n....

Please advise.

Thanks!
0
Comment
Question by:dfernan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36866807
Which version of Python are you using? I don't seem to get that behavior on Linux or Windows. In the Windows version, it printed to the screen with newlines, but in the file, the data was all on one line.
0
 

Author Comment

by:dfernan
ID: 36867911
python 2.6.2 linux...
0
 
LVL 29

Expert Comment

by:pepr
ID: 36890219
I do not believe the code inserted the \n.  I guess that you used the "print element" when printing to console.  The print adds the \n automatically, unless you add a comma after the last argumens. Or you can use the sys.stdout.write() to write exactly what you want.  Try the following:

b.py
import os
import sys

fname = 'foo.txt'

# Just to be sure you are not looking at old results ;)
if os.path.isfile(fname):
    os.remove(fname)

# You should not use the 'file' identifier for your variables.
# You should not use the 'list' identifier for your variables.  It masks the
# built-in list() function.
f = open(fname, 'w')
lst = ['hi', 'how', 'are', 'you']
for element in lst:
    f.write(element)    # this will never write \n to your output
f.close()               # you should always close the open file

# The same to stdout.
for element in lst:
    sys.stdout.write(element)    # this will never write \n to your output

Open in new window


It prints on my Windows console (but it definitely must be the same on a Linux console):
c:\tmp\___python\dfernan\Q_27366174>python b.py
hihowareyou

Open in new window


The same is in the file.
0
 
LVL 29

Accepted Solution

by:
pepr earned 250 total points
ID: 36890840
Or there may be another reason for making you believe that the f.write() adds the \n.  You may have obtained your lst via fin.readline() where fin is the text file opened for reading.  When reading text files in Python, you get the lines that contain also the final \n.  If this is the case, then you have just got the copy of the existing \n's.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question