回答編集履歴
1
修正
answer
CHANGED
@@ -4,20 +4,21 @@
|
|
4
4
|
---
|
5
5
|
上書きの場合はちょっと面倒なので、私はだいたい新しいファイルを作ってから置き換えます。
|
6
6
|
```Python
|
7
|
-
import
|
7
|
+
from pathlib import Path
|
8
8
|
|
9
|
-
src_file = 'test.txt'
|
9
|
+
src_file = Path('test.txt')
|
10
|
-
dst_file = '
|
10
|
+
dst_file = Path('_work.tmp')
|
11
11
|
|
12
|
-
with open(
|
12
|
+
with src_file.open() as fin, \
|
13
|
-
open(
|
13
|
+
dst_file.open(mode='x') as fout:
|
14
14
|
|
15
|
-
|
15
|
+
fout.write(
|
16
|
-
|
16
|
+
fin.read().replace('テスト', '試み')
|
17
|
-
|
17
|
+
)
|
18
18
|
|
19
|
+
|
19
|
-
|
20
|
+
dst_file.replace(
|
20
|
-
|
21
|
+
src_file
|
21
22
|
)
|
22
23
|
```
|
23
24
|
|