Ich studiere Python-Datei-I/O. Ich machte ein einfaches Programm (main.py).Python-Datei lesen, Zeile für Zeile schreiben
Mein Ziel wird Zeile für Zeile gelesen und Zeile für Zeile geschrieben.
fstream = open("input2.txt", 'r');
line = fstream.readline()
while line:
print(line);
line = fstream.readline()
fstream.close()
unten sind meine input2.txt
Datei
start.
hello world.
hello python.
I am studying file I/O in python
end.
wenn ich laufe Python-Programm
Python main.py
Dann Ergebnis ist ...
start.
hello world.
hello python.
I am studying file I/O in python
end.
Das ist nicht das gleiche wie ich erwartet hatte.
So modifizierte ich die main.py
fstream = open("input2.txt", 'r');
line = fstream.read().split("\n")
while line:
print(line);
line = fstream.read().split("\n")
fstream.close()
aber mein Programm in Endlosschleife diged.
Um dieses Problem zu lösen, was soll ich tun?
Das Ergebnis, das ich erwartet habe, ist das folgende.
start.
hello world.
hello python.
I am studying file I/O in python
end.
'Import sys',' sys.stdout.write' statt 'print' –
Versuchen' rstrip() '. –
Geben Sie keinen Python-Code ohne Einzug ein. Schlecht eingerückter Python-Code ist Unsinn. – khelwood