---
source: crates/ruff_linter/src/rules/refurb/mod.rs
---
FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("file.txt").write_text("test")`
 --> FURB103_1.py:3:6
  |
1 | from pathlib import Path
2 |
3 | with Path("file.txt").open("w") as f:
  |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 |     f.write("test")
  |
help: Replace with `Path("file.txt").write_text("test")`
  |
2 |
  - with Path("file.txt").open("w") as f:
  -     f.write("test")
3 + Path("file.txt").write_text("test")
4 |
  |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("file.txt").write_bytes(b"test")`
 --> FURB103_1.py:6:6
  |
4 |     f.write("test")
5 |
6 | with Path("file.txt").open("wb") as f:
  |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 |     f.write(b"test")
  |
help: Replace with `Path("file.txt").write_bytes(b"test")`
  |
5 |
  - with Path("file.txt").open("wb") as f:
  -     f.write(b"test")
6 + Path("file.txt").write_bytes(b"test")
7 |
  |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("file.txt").write_text("test")`
  --> FURB103_1.py:9:6
   |
 7 |     f.write(b"test")
 8 |
 9 | with Path("file.txt").open(mode="w") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 |     f.write("test")
   |
help: Replace with `Path("file.txt").write_text("test")`
   |
8  |
   - with Path("file.txt").open(mode="w") as f:
   -     f.write("test")
9  + Path("file.txt").write_text("test")
10 |
   |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("file.txt").write_text("test", encoding="utf8")`
  --> FURB103_1.py:12:6
   |
10 |     f.write("test")
11 |
12 | with Path("file.txt").open("w", encoding="utf8") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13 |     f.write("test")
   |
help: Replace with `Path("file.txt").write_text("test", encoding="utf8")`
   |
11 |
   - with Path("file.txt").open("w", encoding="utf8") as f:
   -     f.write("test")
12 + Path("file.txt").write_text("test", encoding="utf8")
13 |
   |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("file.txt").write_text("test", errors="ignore")`
  --> FURB103_1.py:15:6
   |
13 |     f.write("test")
14 |
15 | with Path("file.txt").open("w", errors="ignore") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16 |     f.write("test")
   |
help: Replace with `Path("file.txt").write_text("test", errors="ignore")`
   |
14 |
   - with Path("file.txt").open("w", errors="ignore") as f:
   -     f.write("test")
15 + Path("file.txt").write_text("test", errors="ignore")
16 |
   |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path(foo()).write_text("test")`
  --> FURB103_1.py:18:6
   |
16 |     f.write("test")
17 |
18 | with Path(foo()).open("w") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^
19 |     f.write("test")
   |
help: Replace with `Path(foo()).write_text("test")`
   |
17 |
   - with Path(foo()).open("w") as f:
   -     f.write("test")
18 + Path(foo()).write_text("test")
19 |
   |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `p.write_text("test")`
  --> FURB103_1.py:22:6
   |
21 | p = Path("file.txt")
22 | with p.open("w") as f:
   |      ^^^^^^^^^^^^^^^^
23 |     f.write("test")
   |
help: Replace with `p.write_text("test")`
   |
21 | p = Path("file.txt")
   - with p.open("w") as f:
   -     f.write("test")
22 + p.write_text("test")
23 |
   |

FURB103 [*] `Path.open()` followed by `write()` can be replaced by `Path("foo", "bar", "baz").write_text("test")`
  --> FURB103_1.py:25:6
   |
23 |     f.write("test")
24 |
25 | with Path("foo", "bar", "baz").open("w") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26 |     f.write("test")
   |
help: Replace with `Path("foo", "bar", "baz").write_text("test")`
   |
24 |
   - with Path("foo", "bar", "baz").open("w") as f:
   -     f.write("test")
25 + Path("foo", "bar", "baz").write_text("test")
   |
