I am trying to rename multiple images using os.rename()
in Python from a
list keyword_name
randomly so I break it into these steps:
def keyword()
- keyword_name a list that holds keyword
- loop through list
- assign a variable that holds the index(list[])
import os
keyword_name = "key.jpg","alph.jpg","hold.jpg","name.jpg","kid.jpg",
"young","zolo","lima0","sofia","temra","tooad","aeder","ed","de","cf","fc"
def keyword():
index = 0
while index < len(keyword_name):
keyword = keyword_name[index]
print(keyword)
index += 1
- def
rename_name()
- returns a list containing the names of the entries in the directory given by path.
- returns current working directory of a process.
def renam_name():
file_list = os.listdir(r"C:\Desktop\cLine\file")
saved_path =os.getcwd()
os.chdir(r"C:\Desktop\cLine\file")
for f in file_list:
#spilt text to file_name,img_type
file_name, img_type = os.path.splitext(f)
#convert the tuple to a list
keyword = list(file_name)
#join the list
name = "".join(keyword)
print(name)
os.rename(name, keyword_name)
I am new to programming. I did some research, but I only found how to remove integers or a string from file_name
. I'll be so thankful if somebody can help me.
The sources I've been looking at: