execute script in certain directory

B

brad

When I use idle or a shell to execute a python script, the script
executes in the directory it is currently in (in this case, my desktop).
However, when using GNOME and right clicking the py script and selecting
'open with python', the execution occurs in my home directory, not my
desktop.

Is there a way to force py scripts to always run within the directory
that they reside in?

Thanks

Brad

/home/brad/Desktop/output - python from shell
/home/brad/Desktop/output - python from idle
/home/brad/output - python from Gnome 'right click' open with menu
 
A

Alex Popescu

When I use idle or a shell to execute a python script, the script
executes in the directory it is currently in (in this case, my desktop).
However, when using GNOME and right clicking the py script and selecting
'open with python', the execution occurs in my home directory, not my
desktop.

Is there a way to force py scripts to always run within the directory
that they reside in?

Thanks

Brad

/home/brad/Desktop/output - python from shell
/home/brad/Desktop/output - python from idle
/home/brad/output - python from Gnome 'right click' open with menu

Interesting. I was wondering about the opposit: being in the parent
dir, how can I run a module from a package. (the current behavior when
running python dir_name\module.py is to consider the dir_name the
current dir and this breaks all imports). I am pretty sure this is
answered somewhere, but I must confess that so far I haven't been able
to find it :-(.

TIA,

../alex
 
V

vasudevram

When I use idle or a shell to execute a python script, the script
executes in the directory it is currently in (in this case, my desktop).
However, when using GNOME and right clicking the py script and selecting
'open with python', the execution occurs in my home directory, not my
desktop.

Is there a way to force py scripts to always run within the directory
that they reside in?

Thanks

Brad

/home/brad/Desktop/output - python from shell
/home/brad/Desktop/output - python from idle
/home/brad/output - python from Gnome 'right click' open with menu

Any program that runs has a concept of a "current directory". All its
work is done relative to that, unless you open files with absolute
paths.

Don't know if there's a generic way to do what you want. (There may
be, just can't think of it right now). But there are specific ways -
script-specific, that is:

Add these lines to the top of your script:

import os
os.chdir(rundir)

# Replace rundir above with the absolute path (in quotes) of whatever
directory you want the script to have as its current directory when it
runs.

Looks like GNOME is doing a chdir to your home directory (probably for
convenience or security reasons) before running your script, thats why
you see that behaviour. This is why I say there may not be an easy
generic way to do it - because it would involve modifying all possible
execution environments from which your script could be launched. E.g.:
even if you modify GNOME to do what you want, how about if tomorrow
someone else wants to run your script from KDE or some other window
manager? these could do things differently.

HTH
Vasudev Ram
http://www.dancingbison.com
http://jugad.livejournal.com
http://sourceforge.net/projects/xtopdf
 
G

Gabriel Genellina

Add these lines to the top of your script:

import os
os.chdir(rundir)

I usually don't do that, because it invalidates any filename arguments the
program may have.
Instead, I use an explicit directory when I want it. For example, to open
a .dat file located in the same directory as the module using it:

# top of the module
dat_path = os.path.dirname(os.path.abspath(__file__))

# when opening the file
f = open(os.path.join(dat_path, "filename.dat"))
 
G

Gabriel Genellina

En Mon, 09 Jul 2007 14:09:40 -0300, Alex Popescu
Interesting. I was wondering about the opposit: being in the parent
dir, how can I run a module from a package. (the current behavior when
running python dir_name\module.py is to consider the dir_name the
current dir and this breaks all imports). I am pretty sure this is
answered somewhere, but I must confess that so far I haven't been able
to find it :-(.

python dir_name\module.py does NOT change the current dir. It prepends
dir_name to sys.path, if this is what you mean.
The short answer is: don't place standalone scripts inside a package; see
this thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/c44c769a72ca69fa/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top