Solved

How to use file.write in python without new line

Posted on 2011-09-29
4
469 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
  • 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

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

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - Save to file 4 67
Edit linux file using python 4 58
Need help with python script - Insert data from user input into MS SQL 35 123
yara deployment 8 77
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…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

856 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