I've been trying numerous issues here in stack.overflow to remove the last blank lines from the 2.txt
file (input):
2.txt file:
-11
B1
5
B1
-2
B1
7
B1
-11
B1
9
B1
-1
B1
-3
B1
19
B1
-22
B1
2
B1
1
B1
18
B1
-14
B1
0
B1
11
B1
-8
B1
-15
and the only one that worked using print(line)
was this https://stackoverflow.com/a/6745865/10824251. But when I try to use f.write(line)
rather than print(line)
in my final 2.txt
file (output) is as shown below:
2.txt file final:
-11B15B1-2B17B1-11B19B1-1B1-3B119B1-22B12B11B118B1-14B10B111B1-8B1-15
18
B1
-14
B1
0
B1
11
B1
-8
B1
-15
However, when I use the code using print line)
instead of f.write (line)
, my bash terminal displays output with the last lines deleted (see print(line) result in terminal bash
below) but with deformation equal to 2.txt file final
, ie it works correctly. I have tried to understand what is happening but have not made any progress.
print(line) resut in terminal bash
-11B15B1-2B17B1-11B19B1-1B1-3B119B1-22B12B11B118B1-14B10B111B1-8B1-15
18
B1
-14
B1
0
B1
11
B1
-8
B1
-15
UPDATE:
My script eliminating the last lines of 2.txt
file but deforming the first lines of in the terminal bash:
for line in open('2.txt'):
line = line.rstrip()
if line != '':
print (line)
My script deforming the first lines of 2.txt
fileand also does not delete the last lines as desired in file output 3.txt
:
with open("2.txt",'r+') as f:
for line in open('3.txt'):
line = line.rstrip()
if line != '':
f.write(line)