By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

execute script in certain directory

P: n/a
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
Jul 9 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jul 9, 6:31 pm, brad <byte8b...@gmail.comwrote:
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
--
..w( the_mindstorm )p.
Jul 9 '07 #2

P: n/a
On Jul 9, 8:31 pm, brad <byte8b...@gmail.comwrote:
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
Jul 9 '07 #3

P: n/a
En Mon, 09 Jul 2007 14:14:07 -0300, vasudevram <va********@gmail.com>
escribió:
On Jul 9, 8:31 pm, brad <byte8b...@gmail.comwrote:
>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.

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"))

--
Gabriel Genellina

Jul 9 '07 #4

P: n/a
En Mon, 09 Jul 2007 14:09:40 -0300, Alex Popescu
<th***********************@gmail.comescribió:
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....c769a72ca69fa/

--
Gabriel Genellina

Jul 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.