Advertisement
Yesideez

replacetxt.py

Nov 25th, 2022
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import glob
  4. import os
  5. import sys
  6.  
  7. strPath=""
  8. count=0
  9.  
  10. if sys.argv[1]=="--help":
  11.   print("ReplaceTxt v1.oo (22-Nov-2022) by Zeb/SLP\n")
  12.   print("Usage:",sys.argv[0],"<dir> <oldtxt> <newtxt>")
  13. else:
  14. #  print("Arguments:",len(sys.argv))
  15. #  print("Arguments:",str(sys.argv))
  16.   strPath=sys.argv[1]
  17.   strSource=sys.argv[2]
  18.   strTarget=sys.argv[3]
  19.   if os.path.exists(strPath):
  20.     lstFiles=glob.glob(strPath+"/*.*")
  21.     lstFiles.sort()
  22.     if len(lstFiles)>0:
  23.       print("Processing",len(lstFiles),"files...")
  24.       for old in lstFiles:
  25.         new=old.replace(".CDG",".cdg")
  26.         new=new.replace(".MP3",".mp3")
  27.         if new.find(strSource)>-1:
  28.           new=new.replace(strSource,strTarget)
  29.           count+=1
  30. #          print(f"Replaced '{strSource}' with '{strTarget}' in '{new}'")
  31. #          print("OLD:",old)
  32. #          print("NEW:",new)
  33.           try:
  34.             os.rename(old,new)
  35.           except:
  36.             print(f"ERROR: Unable to rename '{old}' as '{new}'")
  37.       print("Renamed",count,"files.")
  38.     else:
  39.       print("ERROR: Unable to find any files")
  40.   else:
  41.     print(f"ERROR: Directory '{sys.argv[1]}' doesn't exist")
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement