Skip to content

Instantly share code, notes, and snippets.

@lambdan
Created April 3, 2021 09:03
Show Gist options
  • Save lambdan/5dd4850c248198ef9a406fbb2b50c7aa to your computer and use it in GitHub Desktop.
Save lambdan/5dd4850c248198ef9a406fbb2b50c7aa to your computer and use it in GitHub Desktop.
Rename foo_dr.txt to DRX.txt
# usage: python3 foo_dr_renamer.py /path/to/music/folder/
# change ASK_FOR_CONFIRMATION to True if you want to decide manually
import os, sys, shutil
ASK_FOR_CONFIRMATION = False
if len(sys.argv) < 2:
print("no path given")
sys.exit(1)
else:
in_path = os.path.abspath(sys.argv[1])
# find foo_dr files
print("searching for foo_dr.txt's in", in_path, "...")
for path, dirs, files in os.walk(in_path):
for f in files:
if f == "foo_dr.txt":
fullpath = os.path.join(path, f)
# found foo_dr, read in lines
with open(fullpath) as foodr:
lines = foodr.readlines()
# look for DR value in lines
for l in lines:
if "Official DR value:" in l:
# found
value = l.split(": ")[1].rstrip() # split at ": ", second component is DRX
break
new_filename = value + ".txt"
new_filepath = os.path.join(path, new_filename)
print(fullpath, "-->", new_filepath)
if ASK_FOR_CONFIRMATION:
decision = input("ok? y/N")
if decision.lower() == "y":
should_rename = True
print("OK, Renaming")
else:
should_rename = False
else:
should_rename = True
if should_rename:
shutil.move(fullpath, new_filepath)
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment