私はpythonファイルI/Oを勉強しています。私は簡単なプログラム(main.py)を作った。pythonファイルの読み込み、行ごとの書き込み
私の目標は1行ずつ読み込まれ、1行ずつ書き込まれます。以下
fstream = open("input2.txt", 'r');
line = fstream.readline()
while line:
print(line);
line = fstream.readline()
fstream.close()
私input2.txt
ファイル
start.
hello world.
hello python.
I am studying file I/O in python
end.
私はPythonプログラムを実行すると
のpython main.py
はその後、結果は...
でありますstart.
hello world.
hello python.
I am studying file I/O in python
end.
これは私が期待したものと同じではありません。私のプログラムは、無限ループにdigedしかし
は、だから私はmain.pyfstream = open("input2.txt", 'r');
line = fstream.read().split("\n")
while line:
print(line);
line = fstream.read().split("\n")
fstream.close()
を変更しました。
は、私が何をすべきか、この問題を解決するには?
私が期待した結果は次のとおりです。
start.
hello world.
hello python.
I am studying file I/O in python
end.
'輸入sys'、代わりに' 'print' –
てみ' rstrip() 'のsys.stdout.write'。 –
インデントなしでPythonコードを投稿しないでください。ひどくインデントされたPythonコードはナンセンスです。 – khelwood