# I found myself with the need to rename my video # collection. For some reason I decided that using # spaces in a file name is lame....underscores should # always be used. After two or three files of manually # renaming I decided that python could do all the # work for me. import os import glob # My video collection is all matroska files. So # the extension of them all is *.mkv format. files_to_change = '*.mkv' # new and old versions of a space lame_space = ' '; cool_space = '_'; # use glob to gather a list of matching files for f in glob.glob(files_to_change): f2 = f f2 = f2.replace(lame_space, cool_space) # add a little status for instant gratification print 'renaming: ', f, ' -> ', f2 os.rename(f, f2) print 'All Done' ## my output: # # renaming: Me at home.mkv -> Me_at_home.mkv # renaming: Max in kid pool.mkv -> Max_in_kid_pool.mkv # ........ < and so on > # All Done
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label string.replace. Show all posts
Showing posts with label string.replace. Show all posts
Wednesday, February 24, 2010
Python - Bulk rename a directory of files
Labels:
glob,
os,
python,
rename,
string.replace
Thursday, September 17, 2009
Python - string stripping
# string manipulations
# striping characters out of strings
aUrl = "http://www.example.com"
# strip, lstrip, rstrip only remove characters
# on the either edge (strip), the left edge (lstrip),
# and the right edge(rstrip)
print aUrl.strip("htp:/w.com")
#outputs:
# example
print aUrl.lstrip("htp:/")
#outputs:
# www.example.com
print aUrl.rstrip(".com")
#outputs:
# http://www.example
# to strip out all occurances
# of a character use replace()
print aUrl.replace('m', "")
#outputs:
# http://www.exaple.co
Friday, July 10, 2009
Python remove vowels from a sentence
# create a tuple with vowels....yes I include y as a vowel vowels = 'a', 'e', 'i', 'o', 'u', 'y' # movie quote for our sentence sentence = "My name is Werner Brandes, my voice is my password, verify me." # iterate through the tuple of vowels for vowel in vowels: # replace the vowel with nothing sentence = sentence.replace(vowel, '') # display the vowel-less sentence! print sentence 'M nm s Wrnr Brnds, m vc s m psswrd, vrf m.' # additional string functions: http://docs.python.org/library/stdtypes.html # manipulate vowels to create pig latin or double dutch
Labels:
python,
remove,
replace,
string.replace,
vowel
Subscribe to:
Posts (Atom)