Find and rename files using a Python script
Question: I am new to Python coding so here a question. I want to find files that are
called "untitled" with any kind of extension, e.g. jpg, indd, psd. Then rename
them to the date of the current day.

I have tried the following:

    
    
    import os
    
    for file in os.listdir("/Users/shirin/Desktop/Artez"):
        if file.endswith("untitled.*"):
            print(file)
    

When I run the script, nothing happens.


Answer: You might find the
[`glob`](https://docs.python.org/3.5/library/glob.html?highlight=glob.glob#glob.glob)
function more useful in this situation:

    
    
    import glob
    
    for file in glob.glob("/Users/shirin/Desktop/Artez/untitled.*"):
        print(file)
    

Your function does not print anything as there are probably no files ending
with `.*` in the name. The `glob.glob()` function will carry out the file
expansion for you.

You can then use this to do your file renaming as follows:

    
    
    import glob
    import os
    from datetime import datetime
    
    current_day = datetime.now().strftime("%Y-%m-%d")
    
    for source_name in glob.glob("/Users/shirin/Desktop/Artez/untitled.*"):
        path, fullname = os.path.split(source_name)
        basename, ext = os.path.splitext(fullname)
        target_name = os.path.join(path, '{}{}'.format(current_day, ext))
        os.rename(source_name, target_name)
    

