Skip to content

Instantly share code, notes, and snippets.

@Danik
Last active September 8, 2019 10:50
Show Gist options
  • Save Danik/10bc6eb7b9b248feb5a17bdda40a5337 to your computer and use it in GitHub Desktop.
Save Danik/10bc6eb7b9b248feb5a17bdda40a5337 to your computer and use it in GitHub Desktop.
Script to batch rename/copy sequentially named files to names given in an array
# Python 3
import os
import shutil
# Rename files named for example 'tile_0.png', 'tile_1.png'... in orig_files_folder
# to the names given in the des_tile_names array (based on their index in the array)
file_ext = ".png"
orig_files_folder = "exported_tiles"
orig_file_base_name = "tile_"
dest_files_folder = "renamed_tiles"
dest_file_names = [
"grass_top", #tile_0
"grass_side", #tile_1
"grass_bottom", #...
"dirt_top",
"dirt_side",
"dirt_bottom"
]
def main():
if not os.path.exists(orig_files_folder):
print('Folder ' + orig_files_folder + ' not found!')
return
if not os.path.exists(dest_files_folder):
os.mkdir(dest_files_folder)
i = 0
for dest_file_name in dest_file_names:
source_path = os.path.join(orig_files_folder, orig_file_base_name + str(i) + file_ext)
if os.path.exists(source_path):
dest_path = os.path.join(dest_files_folder, dest_file_name + file_ext)
print("Copying '" + source_path + "' to '" + dest_path + "'")
# os.rename(source_path, dest_path) # Rename file
shutil.copy(source_path, dest_path) # Copy file
else:
print('File ' + source_path + ' not found, skipping.')
i+=1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment