---
source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs
---
PTH100 [*] `os.path.abspath()` should be replaced by `Path.resolve()`
  --> import_from.py:9:5
   |
 7 | q = "bar"
 8 |
 9 | a = abspath(p)
   |     ^^^^^^^
10 | aa = chmod(p)
11 | aaa = mkdir(p)
   |
help: Replace with `Path(...).resolve()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
7  | p = "/foo"
8  | q = "bar"
9  |
   - a = abspath(p)
10 + a = pathlib.Path(p).resolve()
11 | aa = chmod(p)
   |
note: This is an unsafe fix and may change runtime behavior

PTH101 `os.chmod()` should be replaced by `Path.chmod()`
  --> import_from.py:10:6
   |
 9 | a = abspath(p)
10 | aa = chmod(p)
   |      ^^^^^
11 | aaa = mkdir(p)
12 | makedirs(p)
   |
help: Replace with `Path(...).chmod(...)`

PTH102 [*] `os.mkdir()` should be replaced by `Path.mkdir()`
  --> import_from.py:11:7
   |
 9 | a = abspath(p)
10 | aa = chmod(p)
11 | aaa = mkdir(p)
   |       ^^^^^
12 | makedirs(p)
13 | rename(p)
   |
help: Replace with `Path(...).mkdir()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
11 | aa = chmod(p)
   - aaa = mkdir(p)
12 + aaa = pathlib.Path(p).mkdir()
13 | makedirs(p)
   |

PTH103 [*] `os.makedirs()` should be replaced by `Path.mkdir(parents=True)`
  --> import_from.py:12:1
   |
10 | aa = chmod(p)
11 | aaa = mkdir(p)
12 | makedirs(p)
   | ^^^^^^^^
13 | rename(p)
14 | replace(p)
   |
help: Replace with `Path(...).mkdir(parents=True)`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
12 | aaa = mkdir(p)
   - makedirs(p)
13 + pathlib.Path(p).mkdir(parents=True)
14 | rename(p)
   |

PTH104 `os.rename()` should be replaced by `Path.rename()`
  --> import_from.py:13:1
   |
11 | aaa = mkdir(p)
12 | makedirs(p)
13 | rename(p)
   | ^^^^^^
14 | replace(p)
15 | rmdir(p)
   |
help: Replace with `Path(...).rename(...)`

PTH105 `os.replace()` should be replaced by `Path.replace()`
  --> import_from.py:14:1
   |
12 | makedirs(p)
13 | rename(p)
14 | replace(p)
   | ^^^^^^^
15 | rmdir(p)
16 | remove(p)
   |
help: Replace with `Path(...).replace(...)`

PTH106 [*] `os.rmdir()` should be replaced by `Path.rmdir()`
  --> import_from.py:15:1
   |
13 | rename(p)
14 | replace(p)
15 | rmdir(p)
   | ^^^^^
16 | remove(p)
17 | unlink(p)
   |
help: Replace with `Path(...).rmdir()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
15 | replace(p)
   - rmdir(p)
16 + pathlib.Path(p).rmdir()
17 | remove(p)
   |

PTH107 [*] `os.remove()` should be replaced by `Path.unlink()`
  --> import_from.py:16:1
   |
14 | replace(p)
15 | rmdir(p)
16 | remove(p)
   | ^^^^^^
17 | unlink(p)
18 | getcwd(p)
   |
help: Replace with `Path(...).unlink()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
16 | rmdir(p)
   - remove(p)
17 + pathlib.Path(p).unlink()
18 | unlink(p)
   |

PTH108 [*] `os.unlink()` should be replaced by `Path.unlink()`
  --> import_from.py:17:1
   |
15 | rmdir(p)
16 | remove(p)
17 | unlink(p)
   | ^^^^^^
18 | getcwd(p)
19 | b = exists(p)
   |
help: Replace with `Path(...).unlink()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
17 | remove(p)
   - unlink(p)
18 + pathlib.Path(p).unlink()
19 | getcwd(p)
   |

PTH109 `os.getcwd()` should be replaced by `Path.cwd()`
  --> import_from.py:18:1
   |
16 | remove(p)
17 | unlink(p)
18 | getcwd(p)
   | ^^^^^^
19 | b = exists(p)
20 | bb = expanduser(p)
   |
help: Replace with `Path.cwd()`

PTH110 [*] `os.path.exists()` should be replaced by `Path.exists()`
  --> import_from.py:19:5
   |
17 | unlink(p)
18 | getcwd(p)
19 | b = exists(p)
   |     ^^^^^^
20 | bb = expanduser(p)
21 | bbb = isdir(p)
   |
help: Replace with `Path(...).exists()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
19 | getcwd(p)
   - b = exists(p)
20 + b = pathlib.Path(p).exists()
21 | bb = expanduser(p)
   |

PTH111 [*] `os.path.expanduser()` should be replaced by `Path.expanduser()`
  --> import_from.py:20:6
   |
18 | getcwd(p)
19 | b = exists(p)
20 | bb = expanduser(p)
   |      ^^^^^^^^^^
21 | bbb = isdir(p)
22 | bbbb = isfile(p)
   |
help: Replace with `Path(...).expanduser()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
20 | b = exists(p)
   - bb = expanduser(p)
21 + bb = pathlib.Path(p).expanduser()
22 | bbb = isdir(p)
   |
note: This is an unsafe fix and may change runtime behavior

PTH112 [*] `os.path.isdir()` should be replaced by `Path.is_dir()`
  --> import_from.py:21:7
   |
19 | b = exists(p)
20 | bb = expanduser(p)
21 | bbb = isdir(p)
   |       ^^^^^
22 | bbbb = isfile(p)
23 | bbbbb = islink(p)
   |
help: Replace with `Path(...).is_dir()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
21 | bb = expanduser(p)
   - bbb = isdir(p)
22 + bbb = pathlib.Path(p).is_dir()
23 | bbbb = isfile(p)
   |

PTH113 [*] `os.path.isfile()` should be replaced by `Path.is_file()`
  --> import_from.py:22:8
   |
20 | bb = expanduser(p)
21 | bbb = isdir(p)
22 | bbbb = isfile(p)
   |        ^^^^^^
23 | bbbbb = islink(p)
24 | readlink(p)
   |
help: Replace with `Path(...).is_file()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
22 | bbb = isdir(p)
   - bbbb = isfile(p)
23 + bbbb = pathlib.Path(p).is_file()
24 | bbbbb = islink(p)
   |

PTH114 [*] `os.path.islink()` should be replaced by `Path.is_symlink()`
  --> import_from.py:23:9
   |
21 | bbb = isdir(p)
22 | bbbb = isfile(p)
23 | bbbbb = islink(p)
   |         ^^^^^^
24 | readlink(p)
25 | stat(p)
   |
help: Replace with `Path(...).is_symlink()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
23 | bbbb = isfile(p)
   - bbbbb = islink(p)
24 + bbbbb = pathlib.Path(p).is_symlink()
25 | readlink(p)
   |

PTH115 [*] `os.readlink()` should be replaced by `Path.readlink()`
  --> import_from.py:24:1
   |
22 | bbbb = isfile(p)
23 | bbbbb = islink(p)
24 | readlink(p)
   | ^^^^^^^^
25 | stat(p)
26 | isabs(p)
   |
help: Replace with `Path(...).readlink()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
24 | bbbbb = islink(p)
   - readlink(p)
25 + pathlib.Path(p).readlink()
26 | stat(p)
   |

PTH116 `os.stat()` should be replaced by `Path.stat()`, `Path.owner()`, or `Path.group()`
  --> import_from.py:25:1
   |
23 | bbbbb = islink(p)
24 | readlink(p)
25 | stat(p)
   | ^^^^
26 | isabs(p)
27 | join(p, q)
   |

PTH117 [*] `os.path.isabs()` should be replaced by `Path.is_absolute()`
  --> import_from.py:26:1
   |
24 | readlink(p)
25 | stat(p)
26 | isabs(p)
   | ^^^^^
27 | join(p, q)
28 | sep.join((p, q))
   |
help: Replace with `Path(...).is_absolute()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
26 | stat(p)
   - isabs(p)
27 + pathlib.Path(p).is_absolute()
28 | join(p, q)
   |

PTH118 `os.path.join()` should be replaced by `Path` with `/` operator
  --> import_from.py:27:1
   |
25 | stat(p)
26 | isabs(p)
27 | join(p, q)
   | ^^^^
28 | sep.join((p, q))
29 | sep.join([p, q])
   |

PTH118 `os.sep.join()` should be replaced by `Path` with `/` operator
  --> import_from.py:28:1
   |
26 | isabs(p)
27 | join(p, q)
28 | sep.join((p, q))
   | ^^^^^^^^
29 | sep.join([p, q])
30 | basename(p)
   |

PTH118 `os.sep.join()` should be replaced by `Path` with `/` operator
  --> import_from.py:29:1
   |
27 | join(p, q)
28 | sep.join((p, q))
29 | sep.join([p, q])
   | ^^^^^^^^
30 | basename(p)
31 | dirname(p)
   |

PTH119 [*] `os.path.basename()` should be replaced by `Path.name`
  --> import_from.py:30:1
   |
28 | sep.join((p, q))
29 | sep.join([p, q])
30 | basename(p)
   | ^^^^^^^^
31 | dirname(p)
32 | samefile(p)
   |
help: Replace with `Path(...).name`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
30 | sep.join([p, q])
   - basename(p)
31 + pathlib.Path(p).name
32 | dirname(p)
   |
note: This is an unsafe fix and may change runtime behavior

PTH120 [*] `os.path.dirname()` should be replaced by `Path.parent`
  --> import_from.py:31:1
   |
29 | sep.join([p, q])
30 | basename(p)
31 | dirname(p)
   | ^^^^^^^
32 | samefile(p)
33 | splitext(p)
   |
help: Replace with `Path(...).parent`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
31 | basename(p)
   - dirname(p)
32 + pathlib.Path(p).parent
33 | samefile(p)
   |
note: This is an unsafe fix and may change runtime behavior

PTH121 `os.path.samefile()` should be replaced by `Path.samefile()`
  --> import_from.py:32:1
   |
30 | basename(p)
31 | dirname(p)
32 | samefile(p)
   | ^^^^^^^^
33 | splitext(p)
34 | with open(p) as fp:
   |
help: Replace with `Path(...).samefile()`

PTH122 `os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`
  --> import_from.py:33:1
   |
31 | dirname(p)
32 | samefile(p)
33 | splitext(p)
   | ^^^^^^^^
34 | with open(p) as fp:
35 |     fp.read()
   |

PTH123 [*] `open()` should be replaced by `Path.open()`
  --> import_from.py:34:6
   |
32 | samefile(p)
33 | splitext(p)
34 | with open(p) as fp:
   |      ^^^^
35 |     fp.read()
36 | open(p).close()
   |
help: Replace with `Path.open()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
34 | splitext(p)
   - with open(p) as fp:
35 + with pathlib.Path(p).open() as fp:
36 |     fp.read()
   |

PTH123 [*] `open()` should be replaced by `Path.open()`
  --> import_from.py:36:1
   |
34 | with open(p) as fp:
35 |     fp.read()
36 | open(p).close()
   | ^^^^
   |
help: Replace with `Path.open()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
36 |     fp.read()
   - open(p).close()
37 + pathlib.Path(p).open().close()
38 |
   |

PTH123 [*] `open()` should be replaced by `Path.open()`
  --> import_from.py:43:10
   |
41 |     from builtins import open
42 |
43 |     with open(p) as _: ...  # Error
   |          ^^^^
   |
help: Replace with `Path.open()`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
43 |
   -     with open(p) as _: ...  # Error
44 +     with pathlib.Path(p).open() as _: ...  # Error
45 |
   |

PTH104 [*] `os.rename()` should be replaced by `Path.rename()`
  --> import_from.py:53:1
   |
51 | file = "file_1.py"
52 |
53 | rename(file, "file_2.py")
   | ^^^^^^
54 |
55 | rename(
   |
help: Replace with `Path(...).rename(...)`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
53 |
   - rename(file, "file_2.py")
54 + pathlib.Path(file).rename("file_2.py")
55 |
   |

PTH104 [*] `os.rename()` should be replaced by `Path.rename()`
  --> import_from.py:55:1
   |
53 | rename(file, "file_2.py")
54 |
55 | rename(
   | ^^^^^^
56 |     # commment 1
57 |     file, # comment 2
   |
help: Replace with `Path(...).rename(...)`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
55 |
   - rename(
   -     # commment 1
   -     file, # comment 2
   -     "file_2.py"
   -     ,
   -     # comment 3
   - )
56 + pathlib.Path(file).rename("file_2.py")
57 |
   |
note: This is an unsafe fix and may change runtime behavior

PTH104 [*] `os.rename()` should be replaced by `Path.rename()`
  --> import_from.py:63:1
   |
61 | )
62 |
63 | rename(file, "file_2.py", src_dir_fd=None, dst_dir_fd=None)
   | ^^^^^^
64 |
65 | rename(file, "file_2.py", src_dir_fd=1)
   |
help: Replace with `Path(...).rename(...)`
   |
4  | from os.path import isabs, join, basename, dirname, samefile, splitext
5  + import pathlib
6  |
--------------------------------------------------------------------------------
63 |
   - rename(file, "file_2.py", src_dir_fd=None, dst_dir_fd=None)
64 + pathlib.Path(file).rename("file_2.py")
65 |
   |
