Use os.path.islink instead of os.path.exists to ensure removal of links if a file is moved or deleted

This commit is contained in:
2023-01-01 16:47:19 -05:00
parent 2a0d55a8db
commit b6da652914
+13 -13
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from argparse import ArgumentParser from argparse import ArgumentParser
from os import remove, scandir, symlink from os import remove, scandir, symlink
from os.path import exists from os.path import islink
from random import choice from random import choice
# argument parsing # argument parsing
@@ -20,13 +20,13 @@ for i in range(1, 9):
# national # national
options = [f.path for f in scandir(sorted_dir + 'National/' + str(i)) if f.is_dir()] options = [f.path for f in scandir(sorted_dir + 'National/' + str(i)) if f.is_dir()]
track = choice(options) track = choice(options)
if exists(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.database"): if islink(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.database"):
remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.database") remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.database")
if exists(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.level"): if islink(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.level"):
remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.level") remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.level")
if exists(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.package"): if islink(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.package"):
remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.package") remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.package")
if exists(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.scene"): if islink(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.scene"):
remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.scene") remove(f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.scene")
symlink(f"{track}/Beta_Nat_Track_Slot_{str(i)}.dx9.database", symlink(f"{track}/Beta_Nat_Track_Slot_{str(i)}.dx9.database",
f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.database") f"{database_dir}Beta_Nat_Track_Slot_{str(i)}.dx9.database")
@@ -39,13 +39,13 @@ for i in range(1, 9):
# supercross # supercross
options = [f.path for f in scandir(sorted_dir + 'Supercross/' + str(i)) if f.is_dir()] options = [f.path for f in scandir(sorted_dir + 'Supercross/' + str(i)) if f.is_dir()]
track = choice(options) track = choice(options)
if exists(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.database"): if islink(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.database"):
remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.database") remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.database")
if exists(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.level"): if islink(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.level"):
remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.level") remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.level")
if exists(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.package"): if islink(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.package"):
remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.package") remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.package")
if exists(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.scene"): if islink(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.scene"):
remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.scene") remove(f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.scene")
symlink(f"{track}/Beta_Sx_Track_Slot_{str(i)}.dx9.database", symlink(f"{track}/Beta_Sx_Track_Slot_{str(i)}.dx9.database",
f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.database") f"{database_dir}Beta_Sx_Track_Slot_{str(i)}.dx9.database")
@@ -58,13 +58,13 @@ for i in range(1, 9):
# free ride # free ride
options = [f.path for f in scandir(sorted_dir + 'Free Ride/' + str(i)) if f.is_dir()] options = [f.path for f in scandir(sorted_dir + 'Free Ride/' + str(i)) if f.is_dir()]
track = choice(options) track = choice(options)
if exists(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.database"): if islink(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.database"):
remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.database") remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.database")
if exists(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.level"): if islink(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.level"):
remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.level") remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.level")
if exists(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.package"): if islink(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.package"):
remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.package") remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.package")
if exists(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.scene"): if islink(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.scene"):
remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.scene") remove(f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.scene")
symlink(f"{track}/Beta_Track_Slot_{str(i)}.dx9.database", symlink(f"{track}/Beta_Track_Slot_{str(i)}.dx9.database",
f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.database") f"{database_dir}Beta_Track_Slot_{str(i)}.dx9.database")