python's method of eliminating prefix renaming

  • 2020-12-21 18:06:45
  • OfStack

● Script usage

Iterate over the files under the folder, eliminating the feature string before the file name.

● Script implementation


import os,sys
import re
from string import Template
 
div = r" , "
 
###############################################################################
#  @Function: description
###############################################################################
def do_rename(old,new):
 dirname = os.getcwd()
 os.rename(os.path.join(dirname,old),os.path.join(dirname,new))
 
def rename_file(fname):
 ret = re.search("(^\d+)",fname.strip())
 if ret:
  new_name = fname[ret.end() + 2:]
  do_rename(fname,new_name)
###############################################################################
#  @Function: Main Function
###############################################################################
L = [x for x in os.listdir(os.getcwd())]
map(rename_file,L)

Related articles: