Rename all layer names with Uppercase

Hello all,

I m trying to create a python script which will turn all layer names to uppercase
When sublayers exist i get the following error

Message: 0000_REFERENCE::0001_frameline does not exist in LayerTable

import rhinoscriptsyntax as rs

def LayerNameUppercase():
    layers = rs.LayerNames()
    if layers:
        for layer in layers:
            layer_name_upper = layer.upper()
            rs.RenameLayer(layer, capital_layer_name)
            
            
if __name__ == "__main__":     
    LayerNameUppercase()

Does anyone know why that happens?

import scriptcontext as sc
for layer in sc.doc.Layers:
    layer.Name = layer.Name.upper()

Don’t you want this?

import rhinoscriptsyntax as rs

def LayerNameUppercase():
    layers = rs.LayerNames()
    if layers:
        for layer in layers:
            layer_name_upper = layer.upper()
            #rs.RenameLayer(layer, capital_layer_name) <-- wrong name?
            rs.RenameLayer(layer, layer_name_upper)

Sorry, it was a last minute correction to make it readable for the forum and i forgot to update.
So what you have posted gives me the error that i mentioned before

Great that worked with the following modifications:

import scriptcontext as sc

for layer in sc.doc.Layers:
if layer:
    if layer.Name:
        layer.Name = layer.Name.upper()

Dunno, seems to work here in a quick test…

Need to be careful with sublayers, because if you rename the parent layer before the sublayers it could cause problems.

I think that this was the case.
The solution that @Mahdiyar gave works perfect!
Thanks again!