ip.txt
127.0.0.1
127.0.0.10
127.0.0.15
Code
user@linux:~$ cat script01.py
with open('ip.txt', 'r') as f:
for line in f:
print(line)
user@linux:~$
Output
user@linux:~$ python script01.py
127.0.0.1
127.0.0.10
127.0.0.15
user@linux:~$
One of the solution provided to remove additional new line was to use sys.stdout.write instead of print
Python is adding extra newline to the output
New code with sys.stdout.write
user@linux:~$ cat script02.py
import sys
with open('ip.txt', 'r') as f:
for line in f:
# print(line)
sys.stdout.write(line)
user@linux:~$
However, sys.stdout.write delete the last line.
user@linux:~$ python script02.py
127.0.0.1
127.0.0.10
127.0.0.15user@linux:~$
How to fix this problem?