Use the isidentifier()
method to check a valid identifier.
A valid identifier has only letters, digits, and underscores.
text = 'file1'
print(text.isidentifier())
#output: True
text = '1file'
print(text.isidentifier())
#output: False
text = 'file1_final'
print(text.isidentifier())
#output: True
text = 'file1 final'
print(text.isidentifier())
#output: False
Notice that an identifier must not start with a number or have spaces.