File size: 1,568 Bytes
c2cc76d 516099b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#!/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)
|