Skip to content

Instantly share code, notes, and snippets.

@qiyuangong
Created October 12, 2016 14:37
Show Gist options
  • Save qiyuangong/3f720ab16f1f313e6f0c66548afa92a3 to your computer and use it in GitHub Desktop.
Save qiyuangong/3f720ab16f1f313e6f0c66548afa92a3 to your computer and use it in GitHub Desktop.
Rename nature numbered files with fixed size number
import os
def rename_nature_with_fixed_size(size=3, sep='_'):
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if sep in f and f[0] != sep:
new_name = f.split(sep)
if len(new_name[0]) < size:
new_name[0] = '0' * (size - len(new_name[0])) + new_name[0]
os.rename(f, sep.join(new_name))
if __name__ == "__main__":
rename_nature_with_fixed_size()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment