Solved

Edit/read configuration text file within Python

Posted on 2007-12-04
3
2,796 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 29

Accepted Solution

by:
pepr earned 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Merge lists using index in Python 1 96
Python variable _ manually assigned 9 90
awk sed 8 65
troubleshoot a python script 8 21
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

860 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