(aaa.txt)
1 aaa
2 bbb
3 ccc
4 ddd
aaa.txtの2行目から出力するために
python
1path2 = 'aaa.txt' 2 3with open(path2) as f3: 4 next(f3) 5 for line3 in f3: 6 print(line3.strip("\n")) 7
と書きました。
(実行結果)
2 bbb
3 ccc
4 ddd
この実行結果をそのままline2_2.logというファイルに出力したいです。
python
1with open(path2) as f3: 2 next(f3) 3 for line3 in f3: 4 fileobj = open("line2_2.log","a") 5 fileobj.write(line3.strip("\n"))
とすると、改行されずに
2 bbb 3 ccc 4 ddd
となってしまいます。
また
python
1with open(path2) as f3: 2 next(f3) 3 for line3 in f3: 4 fileobj = open("line2_2.log","a") 5 print(line3.strip("\n"), file=fileobj) 6
としたところ、
python
1print(line3.strip("\n"), file=fileobj) 2 ^ 3SyntaxError: invalid syntax 4
というエラーが出ました。
よろしくお願いします。
回答3件
あなたの回答
tips
プレビュー