variable creation from a pathname

Mark McEahern mark at mceahern.com
Sun Feb 1 18:24:44 CET 2004


gimar wrote:

>I wonder if there's a way to assign a value to the object obtained with:
>
>from os.path import *
>splitext(basename("c:\pyfiles\sample.txt"))[0]    -----> sample
>
>and make it possible something like this, for example:
>
>sample = 123
>
varname = 'sample'
value = 123
statement = '%s = %s' % (varname, value)
exec(statement) # usual caveats about the danger of exec, blah, blah, blah
print locals()

// m




More information about the Python-list mailing list