Writing and reading variables to/from flat file

Discussion in 'Python' started by Kevin Walzer, Dec 14, 2006.

  1. Kevin Walzer

    Kevin Walzer Guest

    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
     
    Kevin Walzer, Dec 14, 2006
    #1
    1. Advertising

  2. "Kevin Walzer" <> wrote in message
    news:84548$45816e6c$4275d90a$...
    >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
     
    Geoffrey Clements, Dec 14, 2006
    #2
    1. Advertising

  3. Bruno Desthuilliers, Dec 14, 2006
    #3
  4. On Thu, 14 Dec 2006 10:31:56 -0500, Kevin Walzer <>
    declaimed the following in gmane.comp.python.general:

    > 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Dec 15, 2006
    #4
  5. 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/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
     
    Jonathan Curran, Dec 15, 2006
    #5
  6. 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 <> 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 Kevinhttp://www.codebykevin.com
     
    Caleb Hattingh, Dec 15, 2006
    #6
  7. Kevin Walzer

    Guest

    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, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,453
    HNguyen
    Dec 21, 2004
  2. R. P.
    Replies:
    3
    Views:
    8,335
    Joe Kesselman
    Jun 22, 2006
  3. eddieb7
    Replies:
    2
    Views:
    339
    eddieb7
    Jul 26, 2006
  4. c676228
    Replies:
    3
    Views:
    375
    Mike Brind [MVP]
    Jul 17, 2008
  5. Replies:
    2
    Views:
    145
    Dan Anderson
    Nov 21, 2003
Loading...

Share This Page