Sorry, but I can't figure this out from the Python documentation or any of the stuff I've found from Google.
So, I've been working on renaming files with code 99% from one of the awesome helpers here at StackOverflow.
I'm working on putting together a renaming script that (and this is what I got help with from someone here) works with the name (not the extension).
I'm sure I'll come up with more replacements, but my problem at the moment is that I can't figure out how to do more than one re.sub. Current Code (Replaces dots with spaces):
import os, shutil, re
def rename_file (original_filename):
name, extension = os.path.splitext(original_filename)
#Remove Spare Dots
modified_name = re.sub("\.", r" ", name)
new_filename = modified_name + extension
try:
# moves files or directories (recursively)
shutil.move(original_filename, new_filename)
except shutil.Error:
print ("Couldn't rename file %(original_filename)s!" % locals())
[rename_file(f) for f in os.listdir('.') if not f.startswith('.')]
Hoping to also
re.sub("C126", "Perception", name)
re.sub("Geo1", "Geography", name)
Also, it'd be awesome if I could have it capitalize the first letter of any word except "and|if"
I tried
modified_name = re.sub("\.", r" ", name) && re.sub(...
but that didn't work; neither did putting them on different lines. How do I do all the subs and stuff I want to do/make?