471,305 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

reading raw variables from file

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
6 1442
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
"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
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
"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
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
"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.

Similar topics

4 posts views Thread by Xah Lee | last post: by
7 posts views Thread by Santah | last post: by
7 posts views Thread by Daniel Moree | last post: by
1 post views Thread by Andrea Gavana | last post: by
4 posts views Thread by isaac2004 | last post: by
6 posts views Thread by efrenba | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.