Advertisement
sawczakl

zfill filenames that begin with numbers

Aug 14th, 2024 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | Software | 0 0
  1. from pathlib import Path
  2. import os
  3.  
  4. folder = input('folder relative to this script: ')
  5. ext = input('extension (don\'t include period): ')
  6. target = int(input('# digits: '))
  7.  
  8. for path in (Path('.') / folder).glob(f'*.{ext}'):
  9.     n, *rest = path.stem.split(' ', 1)
  10.     rest = ''.join(rest)
  11.     n = n.zfill(target)
  12.     name = f'{n} {rest}'.strip() + f'.{ext}'
  13.     os.rename(path, path.parent / name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement