Batch Renaming of Files in a Directory
问题 Is there an easy way to rename a group of files already contained in a directory, using Python? Example: I have a directory full of *.doc files and I want to rename them in a consistent way. X.doc -> \"new(X).doc\" Y.doc -> \"new(Y).doc\" 回答1: Such renaming is quite easy, for example with os and glob modules: import glob, os def rename(dir, pattern, titlePattern): for pathAndFilename in glob.iglob(os.path.join(dir, pattern)): title, ext = os.path.splitext(os.path.basename(pathAndFilename)) os