Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, sys
- from os import listdir
- from os.path import isfile, join
- def rename_all_files(rel_path='', name_style="img_{}"):
- """
- Rename all files in the folder under rel_path.
- Keyword arguments:
- rel_path -- relative path to the folder (default 0.0)
- name_style -- file naming convention with incrementing integer (default "img_{}")
- """
- dir_path = os.path.abspath(os.getcwd())
- full_path = os.path.join(dir_path, rel_path)
- onlyfiles = [f for f in listdir(full_path) if isfile(join(full_path, f))]
- i = 1
- for file in onlyfiles:
- infile = os.path.join(full_path, file)
- outfile = name_style.format(i) + ".jpg"
- if infile != outfile:
- os.rename(infile, os.path.join(full_path, outfile))
- i = i + 1
- else:
- print('Could not rename matching names {} and {}'.format(infile, outfile))
Add Comment
Please, Sign In to add comment