Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.

Commit 98b126e

Browse files
author
Serene-Arc
committed
Convert some os functions to pathlib
1 parent 49ce7f0 commit 98b126e

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

bak/commands/__init__.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def show_bak_list(filename: Optional[Path] = None,
143143
db_handler.get_all_entries()
144144

145145
console = Console()
146-
if bakfiles == []:
146+
if not bakfiles:
147147
console.print(f"No .bakfiles found for "
148148
f"{filename}" if
149149
filename else "No .bakfiles found")
@@ -163,7 +163,7 @@ def show_bak_list(filename: Optional[Path] = None,
163163
i = 1
164164
for _bakfile in bakfiles:
165165
table.add_row(str(i),
166-
os.path.relpath(filename) if
166+
filename.relative_to(Path.cwd()) if
167167
relative_paths else
168168
_bakfile.orig_abspath,
169169
_bakfile.date_created.split('.')[0],
@@ -234,7 +234,7 @@ def bak_down_cmd(filename: Path,
234234
when there are multiple .bakfiles of `filename`)
235235
236236
Args:
237-
filename (str|os.path)
237+
filename (str|Path)
238238
keep_bakfile (bool): If False, .bakfile is deleted (default: False)
239239
quiet (bool): If True, does not ask user to confirm
240240
destination (None|Path): destination path to restore to
@@ -268,20 +268,17 @@ def bak_down_cmd(filename: Path,
268268
if not destination:
269269
destination = bakfile_entry.orig_abspath
270270
if not keep_bakfile:
271-
os.rename(bakfile_entry.bakfile_loc, destination)
271+
bakfile_entry.bakfile_loc.rename(destination)
272272
for entry in bakfile_entries:
273-
# bakfile_entry's bakfile has already been moved
274-
# trying to rm it would print a failure
275-
if entry != bakfile_entry:
276-
os.remove(entry.bakfile_loc)
273+
Path(entry.bakfile_loc).unlink(missing_ok=True)
277274
db_handler.del_bakfile_entry(entry)
278275
else:
279276
copy2(bakfile_entry.bakfile_loc, destination)
280277

281278

282279
def __remove_bakfiles(bakfile_entries):
283280
for entry in bakfile_entries:
284-
os.remove(entry.bakfile_loc)
281+
Path(entry.bakfile_loc).unlink()
285282
db_handler.del_bakfile_entry(entry)
286283

287284

@@ -327,7 +324,7 @@ def bak_print_cmd(bak_to_print: (str, bakfile.BakFile),
327324
"C"))
328325
if _bak_to_print is None:
329326
console.print(
330-
f"No bakfiles found for {os.path.abspath(bak_to_print)}")
327+
f"No bakfiles found for {Path(bak_to_print).resolve()}")
331328
else:
332329
bak_to_print = _bak_to_print
333330
if not isinstance(bak_to_print, bakfile.BakFile):
@@ -345,7 +342,7 @@ def bak_getfile_cmd(bak_to_get: (str, bakfile.BakFile)):
345342
filename = bak_to_get
346343
bak_to_get = _get_bakfile_entry(bak_to_get, err=True)
347344
if bak_to_get is None:
348-
console.print(f"No bakfiles found for {os.path.abspath(filename)}")
345+
console.print(f"No bakfiles found for {Path(filename).resolve()}")
349346
return # _get_bakfile_entry() handles failures, so just exit
350347
print(bak_to_get.bakfile_loc)
351348

0 commit comments

Comments
 (0)