Created
January 13, 2020 16:49
-
-
Save monocongo/870157ffc1b8d146950b1800f0d7881e to your computer and use it in GitHub Desktop.
Rename all files in a directory with a prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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