teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/08/01 07:50

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -10,4 +10,18 @@
10
10
  ```Python
11
11
  with open(rfile) as f:
12
12
  lines_after = [line for line in f if 'XXX' not in line]
13
- ```
13
+ ```
14
+
15
+ おまけ
16
+ ---
17
+ 次のように読み出しと書き出しを同時に行うと、更にメモリ効率が良くなります。
18
+ ```Python
19
+ with open(rfile) as fin, open(wfile) as fout:
20
+ for line in fin:
21
+ if 'XXX' in line:
22
+ continue
23
+
24
+ fout.write(line)
25
+ ```
26
+
27
+ ここまで来るとシェルスクリプトやバッチの方がすっきりしそうなのはさておき。