I am writing xml files and want to add a number to the end of an xml file name if a file with the same name already exists so I get:

```
info_for_john_0.xml
info_for_john_1.xml
info_for_john_2.xml

```

This is my code currently:

```
def pxml(listofdata):
    root = et.Element('INFORMATION')
    et.SubElement(root, 'last_name') = listofdata[0]
    et.SubElement(root, 'phone') = listofdata[1]
    et.SubElement(root, 'email') = listofdata[2]

    tree = et.ElementTree(root)
    ctr = 1
    for i in tree.iter('last_name'): # should be person_id
        name = i.text

    if os.path.exists(f"info_for_{name}_{ctr}.xml") == True:
        tree.write(f"info_for_{name}_{ctr}.xml")
        ctr += 1
    else:
        tree.write(f"info_for_{name}_{ctr}.xml")

```

However, all I am getting is:

```
info_for_john_0.xml
info_for_john_1.xml

```

and after that is just continually rewrites over info\_for\_john\_1.xml. I suspect that is because I declare ctr = 1 in the function so every time it is called the value of ctr gets reset to 1.

I have tried making ctr a global variable and accessing, so like this:

```
ctr = 1

def pxml(listofdata):
    root = et.Element('INFORMATION')
    et.SubElement(root, 'last_name') = listofdata[0]
    et.SubElement(root, 'phone') = listofdata[1]
    et.SubElement(root, 'email') = listofdata[2]

    tree = et.ElementTree(root)
    count = global ctr
    for i in tree.iter('last_name'): # should be person_id
        name = i.text

    if os.path.exists(f"info_for_{name}_{ctr}.xml") == True:
        tree.write(f"info_for_{name}_{ctr}.xml")
        count += 1
    else:
        tree.write(f"info_for_{name}_{ctr}.xml")

```

but I receive a Syntax Error that count = global ctr is invalid syntax. I am unsure how else to access global variables. **--EDIT--** solved below, changed to:

```
global ctr
count = ctr

```