'2.6.5 (r265:79063, Apr 16 2010, 13:57:41) \\n[GCC 4.4.3]'
I have this
#! /usr/bin/env python
f = open('filetest', 'w')
f.write("This is a line")
f.close()
f = open('filetest', 'r')
for i in f.readlines():
print i,
This prints the o/p like this:
$ ./filetest.py
This is a line
abc@abc-ubuntu:~/pythonpractice$
I am wondering why does the prompt go to the newline after "This is a line" is printed? Because cat filestest
gives this:
$ cat filetest
This is a lineabc@abc-ubuntu:~/pythonpractice$
This is standard behavior, afaik. You can use sys.output.write instead, or you can set sys.output.softspace=False to prevent the newline.
See this article for more details: http://code.activestate.com/lists/python-list/419182/
OR you can also use:
#! /usr/bin/env python
from __future__ import print_function
with open('filetest', 'w') as f1:
f1.write("This is a line")
with open('filetest', 'r') as f2:
for line in f2.readlines():
print(line, end='')
from __future__ import print_function
for line in f:
print(line, end="")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.