toolkit / tags2txt_safe
k4d3's picture
update every stupid script
c2cc76d
raw
history blame
1.57 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script walks through a directory and copies .tags files to .txt files, but only
if a .txt file of the same name doesn't already exist.
Usage:
- Place the script in the directory containing the .tags files
- Run the script to copy .tags files to .txt files
- Use the dry_run flag to preview the changes without writing files
Functions:
get_files(path): Walks through the directory and yields .tags files that don't have corresponding .txt files
copy_tags(tags_path, txt_path, dry_run=False): Copies the contents of the .tags file to a new .txt file
"""
from pathlib import Path
import os
import shutil
def get_files(path):
path = Path(path)
# Walk the directory, looking for .tags files
for root, dirs, files in os.walk(path):
root = path / root
for file in files:
file = root / file
if file.suffix != '.tags':
continue
txt = file.with_suffix(".txt")
if txt.exists():
print(f"Skipping {file}: {txt} already exists")
continue
yield file, txt
def copy_tags(tags_path, txt_path, dry_run=False):
if dry_run:
print(f"Would copy {tags_path} to {txt_path}")
else:
shutil.copy2(tags_path, txt_path)
print(f"Copied {tags_path} to {txt_path}")
if __name__ == "__main__":
dry_run = False
for tags_file, txt_file in get_files("."):
copy_tags(tags_file, txt_file, dry_run=dry_run)