Advertisement
Yesideez

DoubleSpaces.py

Nov 15th, 2022
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #PURPOSE: Find double spaces anywhere within a filename and replace with just one
  4.  
  5. import glob
  6. import os
  7. import sys
  8.  
  9. intCount=0
  10. strPath=sys.argv[1]
  11. if os.path.exists(strPath):
  12.   lstFiles=glob.glob(strPath+"/*.cdg") #get the contents of the directory
  13.   lstFiles.sort()
  14.   if len(lstFiles)>0:
  15.     print("Processing",len(lstFiles)*2,"files...")
  16. #    print(lstFiles)
  17.     for i in lstFiles:
  18.       tmp=i.removeprefix(sys.argv[1]+"/") #remove the path
  19.       tmp=tmp.removesuffix(".cdg") #remove the file extension
  20.       pos=tmp.find("  ") #See if there's double spaces within the filename
  21. #      print(f"File: {tmp} ({pos})")
  22.       if pos>=0:
  23.         tmpAR=tmp.split("  ") #split the filename on the two spaces
  24.         os.rename(sys.argv[1]+"/"+tmp+".cdg",sys.argv[1]+"/"+tmpAR[0]+" "+tmpAR[1]+".cdg")
  25.         os.rename(sys.argv[1]+"/"+tmp+".mp3",sys.argv[1]+"/"+tmpAR[0]+" "+tmpAR[1]+".mp3")
  26.         intCount+=1
  27.     if intCount>0:
  28.       print(f"Stripped double spaces from {intCount*2} files.")
  29.   else:
  30.     print("ERROR: Unable to find any cdg files")
  31. else:
  32.   print("ERROR: Directory doesn't exist")
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement