Replace file operations with function

main
bashrc 2026-04-28 17:25:17 +01:00
parent 55b69c0a71
commit 970db19b72
1 changed files with 16 additions and 14 deletions

View File

@ -1841,7 +1841,7 @@ def remove_post_from_index(post_url: str, debug: bool,
""" """
if not os.path.isfile(index_file): if not os.path.isfile(index_file):
return return
post_id: str = remove_id_ending(post_url) post_id: str = remove_id_ending(post_url).strip("\n").strip("\r")
if not text_in_file(post_id, index_file): if not text_in_file(post_id, index_file):
return return
lines: list[str] = \ lines: list[str] = \
@ -1851,19 +1851,21 @@ def remove_post_from_index(post_url: str, debug: bool,
if not lines: if not lines:
return return
try:
with open(index_file, 'w+', encoding='utf-8') as fp_mod2: new_index: str = ''
for line in lines: removed: bool = False
if line.strip("\n").strip("\r") != post_id: for line in lines:
fp_mod2.write(line) if line.strip("\n").strip("\r") != post_id:
continue new_index += line
if debug: continue
print('DEBUG: removed ' + post_id + removed = True
' from index ' + index_file) if debug:
except OSError as exc: print('DEBUG: removed ' + post_id +
print('EX: ' + ' from index ' + index_file)
'remove_post_from_index unable to write ' + if removed:
index_file + ' ' + str(exc)) save_string(new_index, index_file,
'EX: remove_post_from_index unable to write ' +
index_file + ' [ex]')
def remove_moderation_post_from_index(base_dir: str, post_url: str, def remove_moderation_post_from_index(base_dir: str, post_url: str,