variable creation from a pathname

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Sun Feb 1 12:26:32 EST 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

import os
name=os.path.splitext(os.path.basename("c:\pyfiles\sample.txt"))[0]
myvalue = 123
exec("%s=%d" % (name, myvalue))     # <-- this is what you wanted
print sample

(it will print 123).

--Irmen



More information about the Python-list mailing list