Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use file.write in python without new line

Posted on 2011-09-29
4
Medium Priority
?
565 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

670 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