Writing and reading variables to/from flat file

K

Kevin Walzer

I want to write some variables (user preferences, specifically) to a
text file and then read the values from that file.

Here is my code to write the data:

verbosemodes= """
Detailed = "-vv"
Basic = "-q"
"""

file = open('prefs', 'w')

file.writelines(verbosemodes)

file.close()

And here is my code, in a separate module, to read the file and display
the variable values:

readfile = open('prefs').readlines()

for line in readfile:
print line

print Basic


Running the second module yields this error:

Detailed = "-vv"

Basic = "-q"


Traceback (most recent call last):
File "readprefs.py", line 6, in <module>
print Basic
NameError: name 'Basic' is not defined

Clearly the data is getting read (the lines are being printed), but the
variable itself ("Basic") is not being initialized properly. I'm not
sure what I'm doing wrong here--can anyone point me in the right
direction? Thanks.
 
G

Geoffrey Clements

Kevin Walzer said:
I want to write some variables (user preferences, specifically) to a
text file and then read the values from that file.

Here is my code to write the data:

verbosemodes= """
Detailed = "-vv"
Basic = "-q"
"""

file = open('prefs', 'w')

file.writelines(verbosemodes)

file.close()

And here is my code, in a separate module, to read the file and display
the variable values:

readfile = open('prefs').readlines()

for line in readfile:
print line

print Basic


Running the second module yields this error:

Detailed = "-vv"

Basic = "-q"


Traceback (most recent call last):
File "readprefs.py", line 6, in <module>
print Basic
NameError: name 'Basic' is not defined

Clearly the data is getting read (the lines are being printed), but the
variable itself ("Basic") is not being initialized properly. I'm not
sure what I'm doing wrong here--can anyone point me in the right
direction? Thanks.

All you've done is print two strings with the contentents of the two lines
from the file, this does not execute the code in these strings.

Try this:
readfile = open('prefs').readlines()

for line in readfile:
print line
eval(line)
print Basic
 
D

Dennis Lee Bieber

I want to write some variables (user preferences, specifically) to a
text file and then read the values from that file.

Here is my code to write the data:

verbosemodes= """
Detailed = "-vv"
Basic = "-q"
"""
Note that the above is one variable -- verbosemodes -- containing a
multi-line string.
And here is my code, in a separate module, to read the file and display
the variable values:

readfile = open('prefs').readlines()

for line in readfile:
print line
And here you are reading each line of that string; the only variable
here is -- line
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

Jonathan Curran

I want to write some variables (user preferences, specifically) to a
text file and then read the values from that file.

Here is my code to write the data:

verbosemodes= """
Detailed = "-vv"
Basic = "-q"
"""

file = open('prefs', 'w')

file.writelines(verbosemodes)

file.close()

And here is my code, in a separate module, to read the file and display
the variable values:

readfile = open('prefs').readlines()

for line in readfile:
print line

print Basic


Running the second module yields this error:

Detailed = "-vv"

Basic = "-q"


Traceback (most recent call last):
File "readprefs.py", line 6, in <module>
print Basic
NameError: name 'Basic' is not defined

Clearly the data is getting read (the lines are being printed), but the
variable itself ("Basic") is not being initialized properly. I'm not
sure what I'm doing wrong here--can anyone point me in the right
direction? Thanks.

I'm surprised that no one has mentioned the wonderful ConfigParser class.
Documentation is @ http://docs.python.org/lib/module-ConfigParser.html

Straight to the point example @
http://mail.python.org/pipermail/tutor/2001-November/010066.html

This way you can focus on your application instead of dealing with trivial
things such as saving/loading data :)

- Jonathan Curran
 
C

Caleb Hattingh

Hi Kevin

The other posters helped you with configParser, which is what you
wanted, i.e. text file access.

However, you can also get persistance really cheaply with pickling, if
you don't need the saved data to be text-editable:

(from memory)

verboseSettings = {}
verboseSettings['Detailed'] = '-vv'
verboseSettings['Basic'] = '-q'

import cPickle
# Save the data - Just give the dict!
cPickle.dump(verboseSettings, file('prefs','w+'))

# Load the data back - get the dict back
verboseSettings = cPickle.load(file('prefs','r'))

I recently did a ton of scientific data analysis looking for trends in
10 years of data for a petrochemical plant, and I learned just how
convenient dicts and pickles can be to manage one's sanity :)

Caleb
 
B

bearophileHUGS

Geoffrey Clements:
readfile = open('prefs').readlines()
for line in readfile:
print line
eval(line)
print Basic

Instead of using eval, using a plain dict may be a better solution.

Bye,
bearophile
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top