Solved

Edit/read configuration text file within Python

Posted on 2007-12-04
3
2,792 Views
Last Modified: 2012-06-22
Hello,

I am trying to save/read a configuration file from within Python. Here is the situation I am in:

Currently, I am working on a script under Linux, in Python. I want to be able to save some configuration to a file like config.txt. This file would be located in a location similar to:
    /home/[user]/.[application]/config.txt
I also want the Python script to be able to be run from anywhere, eg, from the Desktop, home folder, or any other folder and save and read from the same location.

After some research, I have found that I can use the following code to open a file: f = open("config.txt","w")

The problem is that I can't specify a location. The following code:
    f = open("~/.[program]/config.txt","w")
does not work, because it does not accept ~/

I cannot give a relative address (like .[program]/config.txt if the script was located in the home directory), as the script could be run from anywhere within the users home directory.

I cannot give a full address (not that I am aware of), because I need to have a dynamic username for the [user] field in "/home/[user]/.[program]/config.txt", and I am not aware of how to get the current users username, if it is possible.

What is the best way to save and read the configuration file, if the python script is located anywhere within the users home directory?

Thanks!
0
Comment
Question by:dh-s
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 250 total points
Comment Utility
You probably want to use suitable environment variable and join the user-dependent path with the next parts of the configuration full name. Try something like this:
import os
 

print os.getenv('HOME')

s = os.path.join(os.getenv('HOME'), 'subdir1', 'subdir2', 'config.txt')

print s

Open in new window

0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
You might also be interested in Python standard module ConfigParser -- see http://docs.python.org/lib/module-ConfigParser.html
0
 

Author Closing Comment

by:dh-s
Comment Utility
Thanks. "os.getenv('HOME')" was all I needed/was missing from the code I had before!

Also, thanks for the suggestion of ConfigParser, I will have a look at that when larger configuration files are needed (currently, I am only storing a few lines of text)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now