Last active
August 29, 2015 14:02
-
-
Save chenzx/b39710e922bccd98082a to your computer and use it in GitHub Desktop.
Batch rename files using string.replace & re.sub, Usage: c:\python27\python ..\files_batch_rename.py .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
# | |
import os | |
import sys | |
import os.path | |
import re | |
def filtered(dirpath, filename): | |
#return filename.endswith( ('.avi','.wmv','.mp4','.mkv','.rmvb') ) | |
return filename.endswith( ('.pdf') ) | |
def rename(filename): | |
#fn = filename.replace('+',' ').replace('%5B','[').replace('%5D',']').replace('%28','(').replace('%29',')').replace('%40','@') | |
# #TODO: make a general %xx to char replace? | |
#return re.sub(r'[ ]+', ' ', fn) #not usable as ruby's gsub | |
fn = filename.replace('.pdf',' 2013.pdf').replace('Packt Publishing','Packt') | |
return fn | |
#Use "batch_rename.py ." cannot get 1st argument '.' | |
#But if changed to "C:\Python27\python files_batch_rename.py .", can got the '.', doesn't know why | |
if len(sys.argv)<>2: | |
print "Usgae %s <base_dir>" % (sys.argv[0], sys.argv[1]) | |
sys.exit(0) | |
base_dir = sys.argv[1] | |
recursive = False | |
print "base_dir=[%s], recursive=[%s]" % (base_dir, str(recursive)) | |
for dirpath, subdirnames, filenames in os.walk(base_dir, topdown=True): | |
#os.chdir( dirpath ) | |
#Fuck, why cannot use `cwd`(c will means change)? | |
if not recursive: | |
for filename in filenames: | |
if filtered(dirpath, filename): | |
newfilename = rename(filename) | |
if newfilename!=filename: | |
print "rename %s to %s" % (filename, newfilename) | |
os.rename(filename, newfilename) | |
else: | |
pass #TODO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment