Skip to content

Commit e053f3a

Browse files
committed
modernize some pathlib for linter
1 parent cca2527 commit e053f3a

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

jupyter_core/command.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ def list_subcommands() -> list[str]:
9292
# construct a set of `('foo', 'bar') from `jupyter-foo-bar`
9393
for d in _path_with_self():
9494
try:
95-
names = os.listdir(d)
95+
bin_paths = Path(d).iterdir()
9696
except OSError:
9797
continue
98-
for name in names:
98+
for path in bin_paths:
99+
name = path.name
99100
if name.startswith("jupyter-"):
100101
if sys.platform.startswith("win"):
101102
# remove file-extension on Windows
102-
name = os.path.splitext(name)[0] # noqa: PTH122, PLW2901
103+
name = path.stem
103104
subcommand_tuples.add(tuple(name.split("-")[1:]))
104105
# build a set of subcommand strings, excluding subcommands whose parents are defined
105106
subcommands = set()

jupyter_core/migrate.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,19 @@ def get_ipython_dir() -> str:
8686
def migrate_dir(src: str, dst: str) -> bool:
8787
"""Migrate a directory from src to dst"""
8888
log = get_logger()
89-
if not os.listdir(src):
89+
src_path = Path(src)
90+
dst_path = Path(dst)
91+
if not any(src_path.iterdir()):
9092
log.debug("No files in %s", src)
9193
return False
92-
if Path(dst).exists():
93-
if os.listdir(dst):
94+
if dst_path.exists():
95+
if any(dst_path.iterdir()):
9496
# already exists, non-empty
9597
log.debug("%s already exists", dst)
9698
return False
97-
Path(dst).rmdir()
99+
dst_path.rmdir()
98100
log.info("Copying %s -> %s", src, dst)
99-
ensure_dir_exists(Path(dst).parent)
101+
ensure_dir_exists(dst_path.parent)
100102
shutil.copytree(src, dst, symlinks=True)
101103
return True
102104

@@ -107,19 +109,20 @@ def migrate_file(src: str | Path, dst: str | Path, substitutions: Any = None) ->
107109
substitutions is an optional dict of {regex: replacement} for performing replacements on the file.
108110
"""
109111
log = get_logger()
110-
if Path(dst).exists():
112+
dst_path = Path(dst)
113+
if dst_path.exists():
111114
# already exists
112115
log.debug("%s already exists", dst)
113116
return False
114117
log.info("Copying %s -> %s", src, dst)
115-
ensure_dir_exists(Path(dst).parent)
118+
ensure_dir_exists(dst_path.parent)
116119
shutil.copy(src, dst)
117120
if substitutions:
118-
with Path.open(Path(dst), encoding="utf-8") as f:
121+
with dst_path.open() as f:
119122
text = f.read()
120123
for pat, replacement in substitutions.items():
121124
text = pat.sub(replacement, text)
122-
with Path.open(Path(dst), "w", encoding="utf-8") as f:
125+
with dst_path.open("w") as f:
123126
f.write(text)
124127
return True
125128

0 commit comments

Comments
 (0)