By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

reading raw variables from file

P: n/a
Hi,
I have a file that might contain literal python variable statements at
every line. For example the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load whatever variables
written in it as normal python variable statements so that when i read
the file, my users var will be ["Bob","Jane"] and my status var will be
{1:"ok",2:users[0]} . Is there an easy way of doing this instead of
parsing the files and checking said types?
Thanks
Cheers
Astan
Nov 30 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 30, 4:57 am, Astan Chee <st...@al.com.auwrote:
Hi,
I have a file that might contain literal python variable statements at
every line. For example the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load whatever variables
written in it as normal python variable statements so that when i read
the file, my users var will be ["Bob","Jane"] and my status var will be
{1:"ok",2:users[0]} . Is there an easy way of doing this instead of
parsing the files and checking said types?
Thanks
Cheers
Astan
Think SECURITY. If is someone likely to put malicious code in the file
to crap all over your machine?
If not then import the file as a module.

- Paddy.
Nov 30 '07 #2

P: n/a
"Astan Chee" schrieb
I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?
You might want to look at the eval, exec and execfile;
but bear in in mind Paddy's warning about security.

Regards
Martin
Nov 30 '07 #3

P: n/a
Martin Blume a écrit :
"Astan Chee" schrieb
>I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?

You might want to look at the eval, exec and execfile;
Or just import...
but bear in in mind Paddy's warning about security.
+10

Nov 30 '07 #4

P: n/a
"Bruno Desthuilliers" schrieb
I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?
You might want to look at the eval, exec and execfile;

Or just import...
but bear in in mind Paddy's warning about security.

+10
If I have understood python naming scoping correctly,
doing
my_var="hello"
import stuff
print my_var
is not the same as
my_var="hello"
exec open("stuff.py").read()
print my_var
with stuff.py containing
my_var="bye"

I use this exec open("stuff.py").read() mechanism to set
values in my scripts: the script sets a useful default,
a command-line argument in the form a valid python program
may override it. Why bother with inventing or using another
mechanism when this is perfectly simple, easy and self-explaining?

The one and only thing against it is that a malicious user
can sneak in an os.system("cd / && rm -rf *").

IMHO. YMMV.
Martin


Nov 30 '07 #5

P: n/a
On Nov 30, 10:05 am, "Martin Blume" <mbl...@socha.netwrote:
"Bruno Desthuilliers" schrieb
>I have a file that might contain literal python
>variable statements at every line. For example
>the file info.dat looks like this:
>users = ["Bob", "Jane"]
>status = {1:"ok",2:users[0]}
>the problem is I want to read this file and load
>whatever variables written in it as normal python
>variable statements so that when i read the file,
>my users var will be ["Bob","Jane"] and my status
>var will be {1:"ok",2:users[0]} .
>Is there an easy way of doing this instead of
>parsing the files and checking said types?
You might want to look at the eval, exec and execfile;
Or just import...
but bear in in mind Paddy's warning about security.
+10

If I have understood python naming scoping correctly,
doing
my_var="hello"
import stuff
print my_var
is not the same as
my_var="hello"
exec open("stuff.py").read()
print my_var
with stuff.py containing
my_var="bye"
It's not the same...

from stuff import *

....is.
I use this exec open("stuff.py").read() mechanism to set
values in my scripts: the script sets a useful default,
a command-line argument in the form a valid python program
may override it. Why bother with inventing or using another
mechanism when this is perfectly simple, easy and self-explaining?

The one and only thing against it is that a malicious user
can sneak in an os.system("cd / && rm -rf *").

IMHO. YMMV.
Martin
Regards,
Jordan
Dec 1 '07 #6

P: n/a
"MonkeeSage" schrieb

If I have understood python naming scoping correctly,
doing
my_var="hello"
import stuff
print my_var
is not the same as
my_var="hello"
exec open("stuff.py").read()
print my_var
with stuff.py containing
my_var="bye"

It's not the same...

from stuff import *

...is.
And indeed it is. Thanks.
Martin

Dec 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.