?
Solved

Edit/read configuration text file within Python

Posted on 2007-12-04
3
Medium Priority
?
2,800 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 1000 total points
ID: 20402706
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 29

Expert Comment

by:pepr
ID: 20402723
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
ID: 31412554
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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

777 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