Skip to content

Instantly share code, notes, and snippets.

@jinyu121
Created March 29, 2017 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinyu121/ca84c743e21f5e7507ef6d4747b0eb76 to your computer and use it in GitHub Desktop.
Save jinyu121/ca84c743e21f5e7507ef6d4747b0eb76 to your computer and use it in GitHub Desktop.
将杂乱无章的文件重命名
import re
import os.path
rootdir = "/home/haoyu/pictures"
fn_ext = ".png"
pattern_ith = re.compile(r"\((\d+)\)")
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
ith = pattern_ith.findall(filename)
fn_ori = os.path.join(rootdir, filename)
fn_dst = os.path.join(rootdir, ith[0] + fn_ext)
print(fn_ori,"\n\t==>\t",fn_dst)
os.rename(fn_ori, fn_dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment