473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Writing and reading variables to/from flat file

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.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Dec 14 '06 #1
5 2617

"Kevin Walzer" <kw@codebykevin.comwrote in message
news:84***************************@FUSE.NET...
>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

--
Geoff

Dec 14 '06 #2
Kevin Walzer a écrit :
I want to write some variables (user preferences, specifically) to a
text file and then read the values from that file.
http://docs.python.org/lib/module-ConfigParser.html
Dec 14 '06 #3
On Thursday 14 December 2006 09:31, Kevin Walzer wrote:
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.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
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/tut...er/010066.html

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

- Jonathan Curran
Dec 15 '06 #4
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

On Dec 14, 4:31 pm, Kevin Walzer <k...@codebykevin.comwrote:
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.

--
Kevin Walzer
Code by Kevinhttp://www.codebykevin.com
Dec 15 '06 #5
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

Dec 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
7
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
385
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like...
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: murthydb2 | last post by:
Hi My requirement is that i have to write a stored procedure in db2 and that will be executed in a batch file . Any system error or validation error that occurs inside the db2 sp during...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.