Skip to content

Instantly share code, notes, and snippets.

@monocongo
Created January 13, 2020 16:49
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 monocongo/870157ffc1b8d146950b1800f0d7881e to your computer and use it in GitHub Desktop.
Save monocongo/870157ffc1b8d146950b1800f0d7881e to your computer and use it in GitHub Desktop.
Rename all files in a directory with a prefix
import os
directory = "/home/james/images"
extension = ".jpg"
prefix = "prefix_"
for filename in os.listdir(directory):
if not filename.endswith(extension):
continue
new_filename = prefix + filename
old_file_path = os.path.join(directory, filename)
new_file_path = os.path.join(directory, new_filename)
os.rename(old_file_path, new_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment