On Tuesday, March 15, 2011 9:47:17 AM UTC-4, Mario Rol wrote:
>
> os.path.join(sys.path[0], 'config.txt')
>
> If the script and config.txt are in /tmp this will return '/tmp/
> config.txt' no matter from which directory you started the script.
You have to be careful about symlinks. Even Windows
On Mar 14, 10:25 am, Alexander Schatten wrote:
> Hi,
>
> could someone help me with a small problem? I wrote a Python script
> that does some RegEx... transformations. Now, this script loads some
> configuration data from a file located in the same directory:
>
> open ('config.txt', 'r').
>
> Howe
On Monday, March 14, 2011 5:17:49 PM UTC-4, Grant Edwards wrote:
>
> Indeed that is very common, and there's been a "standard" way to do
> that since before dirt.
>
> The standard on Unix is to look in the following places in (this
> order), and use the first one you find:
On Windows it's typical
On 2011-03-14, Alexander Schatten wrote:
> They don't. Hm, ok, I am always for best practices. If there is a
> better way to do it I am open for suggestions ;-) How would the best
> practice be to load configuration data from a file.
>
> I mean, this is something very common: you write a program
In <5520ec67-bc4e-4f81-b27a-cf1f8c8de...@v11g2000prb.googlegroups.com>
Alexander Schatten writes:
> I mean, this is something very common: you write a program or a script
> and want to load some configuration data.
There are several good ways to do it:
+ Assume the config file is in the curren
You can make it a required input with default values to sys.argv when you
start the program. Parse the content of the file given by sys.argv, then
cache it in some global module or pass it around as arguments...
That said, if you still want to go the route of knowing where the current
directory of
On Mon, Mar 14, 2011 at 11:25 AM, Alexander Schatten wrote:
> They don't. Hm, ok, I am always for best practices. If there is a
> better way to do it I am open for suggestions ;-) How would the best
> practice be to load configuration data from a file.
>
> I mean, this is something very common: yo
They don't. Hm, ok, I am always for best practices. If there is a
better way to do it I am open for suggestions ;-) How would the best
practice be to load configuration data from a file.
I mean, this is something very common: you write a program or a script
and want to load some configuration data
On 2011-03-14, Alexander Schatten wrote:
> Thanks for the comments so far. This sounds to be more complicated in
> detail than I expected. I wonder how all the other Python programs and
> scripts are doing that...
The usual answer is "they don't".
At least in the Unix world, finding out the pat
Alexander Schatten wrote:
> Thanks for the comments so far. This sounds to be more complicated in
> detail than I expected. I wonder how all the other Python programs and
> scripts are doing that...
Well, it's not like that's impossible to find out, the source is out
there! :)
Anyhow, you basical
On Mon, 14 Mar 2011 10:29:42 +, Duncan Booth wrote:
> Steven D'Aprano wrote:
>
>> On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
>>
>>> is there an easy way (API) to get the directory of the currently
>>> running script?
>>
>> import __main__
>> import os
>> print os.path.di
Thanks for the comments so far. This sounds to be more complicated in
detail than I expected. I wonder how all the other Python programs and
scripts are doing that...
--
http://mail.python.org/mailman/listinfo/python-list
On Monday, March 14, 2011 5:53:25 AM UTC-4, Alain Ketterlin wrote:
> sys.path[0] is the path to the directory containing the script that the
> interpreter started with.
How about os.path.dirname(os.path.realpath(sys.argv[0]))? I think realpath is
required in case someone runs it from a symlink.
I tested my solution on python 2.5, 2.8 and 3.1 and everything seems working
well
- Gennadiy
On Mon, Mar 14, 2011 at 4:29 PM, Duncan Booth
wrote:
> Steven D'Aprano wrote:
>
> > On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
> >
> >> is there an easy way (API) to get the direc
Steven D'Aprano wrote:
> On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
>
>> is there an easy way (API) to get the directory of the currently running
>> script?
>
> import __main__
> import os
> print os.path.dirname(__main__.__file__)
>
That code is pretty version specific:
I
Alexander Schatten writes:
> could someone help me with a small problem? I wrote a Python script
> that does some RegEx... transformations. Now, this script loads some
> configuration data from a file located in the same directory:
sys.path[0] is the path to the directory containing the script t
On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
> is there an easy way (API) to get the directory of the currently running
> script?
import __main__
import os
print os.path.dirname(__main__.__file__)
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
To get the directory of the current running script try:
import os.path
path = os.path.dirname(__file__)
- Gennadiy
On Mon, Mar 14, 2011 at 3:25 PM, Alexander Schatten wrote:
> Hi,
>
> could someone help me with a small problem? I wrote a Python script
> that does some RegEx... transformatio
Hi,
could someone help me with a small problem? I wrote a Python script
that does some RegEx... transformations. Now, this script loads some
configuration data from a file located in the same directory:
open ('config.txt', 'r').
However, this only works when I execute the script being in the
dir
19 matches
Mail list logo