variable creation from a pathname

G

gimar

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


Thank you in advance.
 
M

Mark McEahern

gimar said:
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
 
I

Irmen de Jong

gimar said:
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
 
G

gimar

Irmen de Jong said:
gimar said:
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
 
G

gimar

Thank you very much.
If myvalue=123 could be myvalue=[], is string formatting also possible ?

Gpaolo


Irmen de Jong said:
gimar said:
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
 
I

Irmen de Jong

gimar said:
Thank you very much.
If myvalue=123 could be myvalue=[], is string formatting also possible ?

I don't understand, what is it that you want to do exactly?
My code was only an example, you can use any python statement in
the exec() call... So the answer to your question is probably "yes"
but I can't write it down because I don't know what you want to do.

--Irmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top