Skip to content

Instantly share code, notes, and snippets.

@rexlow
Last active November 14, 2018 08:22
Show Gist options
  • Save rexlow/445d2eeb1cd8e649d1ffda36178e01a3 to your computer and use it in GitHub Desktop.
Save rexlow/445d2eeb1cd8e649d1ffda36178e01a3 to your computer and use it in GitHub Desktop.
Simple Python script to rename all files in a directory
# -*- coding: utf-8 -*-
"""
Created on Tuesday Nov 13
This script is to rename all files in a directory according to a pattern without changing file extension
@author: Rex Low
@github: rexlow
"""
import os
def rename(fdir, startIdx):
i = startIdx
for _, f in enumerate(os.listdir(fdir)):
if f.lower().endswith((".jpg", ".png", ".jpeg")):
src = os.path.join(fdir, f)
dst = os.path.join(fdir, "plate{}{}".format(i, os.path.splitext(f)[1]))
os.rename(src, dst)
i += 1
rename("/Users/rex/Desktop/images/crawl", 428)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment