---
source: crates/ruff_linter/src/rules/refurb/mod.rs
---
FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text()`
  --> FURB101_0.py:12:6
   |
11 | # FURB101
12 | with open("file.txt") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^
13 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_text()`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
12 | # FURB101
   - with open("file.txt") as f:
   -     x = f.read()
13 + x = pathlib.Path("file.txt").read_text()
14 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_bytes()`
  --> FURB101_0.py:16:6
   |
15 | # FURB101
16 | with open("file.txt", "rb") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
17 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_bytes()`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
16 | # FURB101
   - with open("file.txt", "rb") as f:
   -     x = f.read()
17 + x = pathlib.Path("file.txt").read_bytes()
18 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_bytes()`
  --> FURB101_0.py:20:6
   |
19 | # FURB101
20 | with open("file.txt", mode="rb") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_bytes()`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
20 | # FURB101
   - with open("file.txt", mode="rb") as f:
   -     x = f.read()
21 + x = pathlib.Path("file.txt").read_bytes()
22 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text(encoding="utf8")`
  --> FURB101_0.py:24:6
   |
23 | # FURB101
24 | with open("file.txt", encoding="utf8") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_text(encoding="utf8")`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
24 | # FURB101
   - with open("file.txt", encoding="utf8") as f:
   -     x = f.read()
25 + x = pathlib.Path("file.txt").read_text(encoding="utf8")
26 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text(errors="ignore")`
  --> FURB101_0.py:28:6
   |
27 | # FURB101
28 | with open("file.txt", errors="ignore") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_text(errors="ignore")`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
28 | # FURB101
   - with open("file.txt", errors="ignore") as f:
   -     x = f.read()
29 + x = pathlib.Path("file.txt").read_text(errors="ignore")
30 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text()`
  --> FURB101_0.py:32:6
   |
31 | # FURB101
32 | with open("file.txt", mode="r") as f:  # noqa: FURB120
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_text()`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
32 | # FURB101
   - with open("file.txt", mode="r") as f:  # noqa: FURB120
   -     x = f.read()
33 + x = pathlib.Path("file.txt").read_text()
34 |
   |
note: This is an unsafe fix and may change runtime behavior

FURB101 `open` and `read` should be replaced by `Path(foo()).read_bytes()`
  --> FURB101_0.py:36:6
   |
35 | # FURB101
36 | with open(foo(), "rb") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^
37 |     # The body of `with` is non-trivial, but the recommendation holds.
38 |     bar("pre")
   |
help: Replace with `Path(foo()).read_bytes()`

FURB101 `open` and `read` should be replaced by `Path("a.txt").read_text()`
  --> FURB101_0.py:44:6
   |
43 | # FURB101
44 | with open("a.txt") as a, open("b.txt", "rb") as b:
   |      ^^^^^^^^^^^^^^^^^^
45 |     x = a.read()
46 |     y = b.read()
   |
help: Replace with `Path("a.txt").read_text()`

FURB101 `open` and `read` should be replaced by `Path("b.txt").read_bytes()`
  --> FURB101_0.py:44:26
   |
43 | # FURB101
44 | with open("a.txt") as a, open("b.txt", "rb") as b:
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^
45 |     x = a.read()
46 |     y = b.read()
   |
help: Replace with `Path("b.txt").read_bytes()`

FURB101 `open` and `read` should be replaced by `Path("file.txt").read_text()`
  --> FURB101_0.py:49:18
   |
48 | # FURB101
49 | with foo() as a, open("file.txt") as b, foo() as c:
   |                  ^^^^^^^^^^^^^^^^^^^^^
50 |     # We have other things in here, multiple with items, but
51 |     # the user reads the whole file and that bit they can replace.
   |
help: Replace with `Path("file.txt").read_text()`

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text(newline="\r\n")`
  --> FURB101_0.py:85:6
   |
84 | # FURB101 (newline is supported in read_text on Python 3.13+)
85 | with open("file.txt", newline="\r\n") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_text(newline="\r\n")`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
85 | # FURB101 (newline is supported in read_text on Python 3.13+)
   - with open("file.txt", newline="\r\n") as f:
   -     x = f.read()
86 + x = pathlib.Path("file.txt").read_text(newline="\r\n")
87 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text(newline="b")`
  --> FURB101_0.py:89:6
   |
88 | # FURB101 (dont mistake "newline" for "mode")
89 | with open("file.txt", newline="b") as f:
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90 |     x = f.read()
   |
help: Replace with `Path("file.txt").read_text(newline="b")`
   |
1  + import pathlib
2  | def foo():
--------------------------------------------------------------------------------
89 | # FURB101 (dont mistake "newline" for "mode")
   - with open("file.txt", newline="b") as f:
   -     x = f.read()
90 + x = pathlib.Path("file.txt").read_text(newline="b")
91 |
   |

FURB101 [*] `open` and `read` should be replaced by `Path("file.txt").read_text(encoding="utf-8")`
   --> FURB101_0.py:130:6
    |
129 | # FURB101
130 | with open("file.txt", encoding="utf-8") as f:
    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131 |     contents: str = f.read()
    |
help: Replace with `Path("file.txt").read_text(encoding="utf-8")`
    |
1   + import pathlib
2   | def foo():
--------------------------------------------------------------------------------
130 | # FURB101
    - with open("file.txt", encoding="utf-8") as f:
    -     contents: str = f.read()
131 + contents: str = pathlib.Path("file.txt").read_text(encoding="utf-8")
132 |
    |

FURB101 `open` and `read` should be replaced by `Path("file.txt").read_text(encoding="utf-8")`
   --> FURB101_0.py:134:6
    |
133 | # FURB101 but no fix because it would remove the assignment to `x`
134 | with open("file.txt", encoding="utf-8") as f:
    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135 |     contents, x = f.read(), 2
    |
help: Replace with `Path("file.txt").read_text(encoding="utf-8")`

FURB101 `open` and `read` should be replaced by `Path("file.txt").read_text(encoding="utf-8")`
   --> FURB101_0.py:138:6
    |
137 | # FURB101 but no fix because it would remove the `process_contents` call
138 | with open("file.txt", encoding="utf-8") as f:
    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139 |     contents = process_contents(f.read())
    |
help: Replace with `Path("file.txt").read_text(encoding="utf-8")`

FURB101 `open` and `read` should be replaced by `Path("file1.txt").read_text(encoding="utf-8")`
   --> FURB101_0.py:141:6
    |
139 |     contents = process_contents(f.read())
140 |
141 | with open("file1.txt", encoding="utf-8") as f:
    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
142 |     contents: str = process_contents(f.read())
    |
help: Replace with `Path("file1.txt").read_text(encoding="utf-8")`
