I want to rename/replace part of the string with a new name.
I have a string that represents a file including its path and file extension as shown below.
source_file = 'images/filename.jpg'
Below shows what I have tried so far. It works, however, is there a better way to produce the same outcome? I define 'better' by, shorter syntax and more efficient.
import uuid
source_file = 'images/filename.jpg'
split = source_file.rsplit('/', 1)
path, filename = split[0], split[1]
ext = filename.rsplit('.', 1)[1]
# rebuild
renamed = path + str(uuid.uuid4()) + ext