-2

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:

  1. 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   
  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:

Rename multiple files in a directory in Python

Rename multiple files in Python

2
  • What is the purpose of the keyword method()? Because it just prints each item from the keyword_name and increments an unused index. Did you intend to get a random name from keyword and use it for os.rename? Commented May 18, 2018 at 2:22
  • yes that exactly what i am trying to do os.rename(name, keyword_name) but i got stuck how to bring keyword[index] +1 and store it to a variable and change to str Commented May 18, 2018 at 2:25

1 Answer 1

0

There are many parts of your code that aren't needed.

The keyword method should randomly select a keyword from keyword_name.
Then, call it to provide the 2nd argument to os.rename.

Try this (I placed comments inline):

import os
import random


# Use a list instead of a tuple so that we can modify
keyword_name = ["key","alph","hold","name","kid", "young","zolo","lima0","sofia","temra","tooad","aeder","ed","de","cf","fc"]


def keyword():
    # Generate a random index
    rand_index = random.randint(0, len(keyword_name)-1)

    # Get the keyword 
    rand_keyword = keyword_name[rand_index]

    # Remove the used-up keyword from the list to
    # prevent randomly selecting it again when renaming
    keyword_name.remove(rand_keyword)

    return rand_keyword


def renam_name():
    os.chdir(r"<path-to-image-folder>")
    file_list = os.listdir(r"<path-to-image-folder>")
    for f in file_list:
        # get the file extension
        file_name, img_type = os.path.splitext(f)   

        # Call the random keyword generator then use the return
        # value as the new file name. 
        os.rename(f, keyword() + img_type)


renam_name()

In addition, since keywords are chosen at random, you'll have to prevent selecting the same keyword, so that the code will not rename the file to a previously renamed file. To do this, I remove the already-chosen keyword from keyword_name (I needed to change keyword_name to a list because tuples are immutable).

It might be also better to remove the ".jpg" extensions from the keyword_name list, as those will produce new filenames like "name.jpg.png".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.