Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import glob
- import os
- import sys
- strPath=""
- count=0
- if sys.argv[1]=="--help":
- print("ReplaceTxt v1.oo (22-Nov-2022) by Zeb/SLP\n")
- print("Usage:",sys.argv[0],"<dir> <oldtxt> <newtxt>")
- else:
- # print("Arguments:",len(sys.argv))
- # print("Arguments:",str(sys.argv))
- strPath=sys.argv[1]
- strSource=sys.argv[2]
- strTarget=sys.argv[3]
- if os.path.exists(strPath):
- lstFiles=glob.glob(strPath+"/*.*")
- lstFiles.sort()
- if len(lstFiles)>0:
- print("Processing",len(lstFiles),"files...")
- for old in lstFiles:
- new=old.replace(".CDG",".cdg")
- new=new.replace(".MP3",".mp3")
- if new.find(strSource)>-1:
- new=new.replace(strSource,strTarget)
- count+=1
- # print(f"Replaced '{strSource}' with '{strTarget}' in '{new}'")
- # print("OLD:",old)
- # print("NEW:",new)
- try:
- os.rename(old,new)
- except:
- print(f"ERROR: Unable to rename '{old}' as '{new}'")
- print("Renamed",count,"files.")
- else:
- print("ERROR: Unable to find any files")
- else:
- print(f"ERROR: Directory '{sys.argv[1]}' doesn't exist")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement