Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Edit/read configuration text file within Python

Posted on 2007-12-04
3
Medium Priority
?
2,801 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…

604 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