Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created June 10, 2015 03:56
Show Gist options
  • Save meysampg/debe3b4c9359038b8287 to your computer and use it in GitHub Desktop.
Save meysampg/debe3b4c9359038b8287 to your computer and use it in GitHub Desktop.
Rename Downloaded file from YouTube with Filegir.com
import os, glob, sys
import urllib.parse as up
# Titles contain name of videos, like ```Supervised and Unsupervised Learning```
fpTitle = open('Titles', 'r')
# listDwn contain name of files that saved from filegir.com, like ```StatsLearning_Lect1_2b_111213_v2_%5BLvaTokhYnDw%5D_%5Btag22%5D.mp4```
fpListDwn = open('listDwn', 'r')
titles = fpTitle.readlines()
i = 1
for fileName in glob.glob("*.mp4*"):
if (fileName[len(fileName)-1] != "4"):
newFileName = fileName[0:len(fileName)-1]
try:
os.rename(fileName, newFileName)
except:
print(sys.exc_info()[0])
# ENDEXCEPTION
# ENDIF
# ENDFOR
for lineNumberOnList, fileName in enumerate(fpListDwn):
fileName = (up.unquote(fileName)).replace("\n", "")
newFileName = titles[lineNumberOnList]
for kindle, repl in [("\n", ""), ("[", "\["), ("]", "\]")]:
newFileName = newFileName.replace(kindle, repl)
# ENDFOR
newFileName = str(i) + " - " + newFileName + ".mp4"
try:
os.rename(fileName, newFileName)
print(fileName + " renamed to " + newFileName)
except:
print(sys.exc_info()[0])
# ENDEXCEPTION
i += 1
# ENDFOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment