Solved

How to use file.write in python without new line

Posted on 2011-09-29
4
424 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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now