for dirpath, _, filenames in os.walk(root_dir): for filename in filenames: if filename.endswith(".md"): file_path = os.path.join(dirpath, filename) withopen(file_path, "r") as file: contents = file.read() matches = re.findall(r"images\/(.+\..+)\)", contents) referenced_files.update(matches)
for filename in os.listdir(directory): file_extension = os.path.splitext(filename)[1].lower() if file_extension in valid_extensions: picture_files.append(filename)
for e in unref_file: try: os.remove(image_dir + e) print(f"Deleted file: {e}") except OSError as e: print(f"Error deleting file: {e} - {e}")
defrename_ref_in_post(oldname, newname): for dirpath, _, filenames in os.walk(post_dir): for filename in filenames: if filename.endswith(".md"): file_path = os.path.join(dirpath, filename) withopen(file_path, "r+") as file: contents = file.read()
contents = contents.replace(oldname, newname)
file.seek(0) file.truncate() file.write(contents)
defrandom_filename(post_dir, image_dir): fms = get_ref_filename(post_dir) for fm in fms: suffix = fm.split(".")[-1] uid = str(uuid.uuid4())[:6] os.rename(f"{image_dir}{fm}", f"{image_dir}{uid}.{suffix}") rename_ref_in_post(fm, f"{uid}.{suffix}")
if __name__ == '__main__': # rename_ref_in_post('image-5-1024x691.png', 'image-5.png') random_filename(post_dir, image_dir)
hinak0
Recording personal notes, covering various aspects.