473,770 Members | 3,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

execute script in certain directory

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
4 2359
On Jul 9, 6:31 pm, brad <byte8b...@gmai l.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
On Jul 9, 8:31 pm, brad <byte8b...@gmai l.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
En Mon, 09 Jul 2007 14:14:07 -0300, vasudevram <va********@gma il.com>
escribió:
On Jul 9, 8:31 pm, brad <byte8b...@gmai l.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.abspat h(__file__))

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

--
Gabriel Genellina

Jul 9 '07 #4
En Mon, 09 Jul 2007 14:09:40 -0300, Alex Popescu
<th************ ***********@gma il.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
11277
by: deko | last post by:
Is there a way to get my script to run on regularly scheduled intervals without using cron? My hosting provider sucks... cannot use cron... I was thinking perhaps I could create a file every hour in a certain directory and then fire the script when the file count reaches a certain number, and then clear out the directory... or something like that. Any suggestions? Thanks in advance.
11
2388
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name (it is operating system dependent whether this is a full pathname or not). So this doesn't seem to be the answer.
9
19123
by: sdb1031 | last post by:
I am trying to run an exe within a python script, but I'm having trouble with spaces in the directory name. The following example will display the usage statement of the program, so I know that the space in the path to the exe is being handled correctly and that the program was executed. CMD= r'"C:\program files\some directory\engine\theexe.exe"' os.system(CMD)
3
6308
by: Jeeran | last post by:
I need to perform url rewriting to convert this (for example): /blogs/feeds/popular/posts/ to this: /blogs/feeds.aspx?type=popular&type2=posts What I did was the following: 1. Created an http handler that parses the url and based on it will execute another aspx page using Server.Execute
5
7526
by: Chandra | last post by:
Hi, Is there a way to execute a python script(file) in ASP.NET application (programmatically)?? Regards, Chandra
19
11378
by: citronelu | last post by:
Is it possible to execute a binary string stored within a python script as executable code ? The script is run under Windows, and the binary code (a full executable file) is stored in a variable in the script. I know I can use os.system() or os.popen() to run an external file, but these functions take as argument a string evaluated as command-line. I also know I could save the binary code as a temporary file, execute
3
4940
by: joe jacob | last post by:
I configured apache to execute python scripts using mod_python handler. I followed below mentioned steps to configure apache. 1. In http.conf I added <Directory "D:/softwares/Apache2.2/htdocs"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
3
5193
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
13
9305
by: scaldo | last post by:
I am just starting out on Python and trying to make a simple interface as practice. Right now I can execute a program through the os.popen command. However, I have to specify the directory where the program is located. For example, say: os.popen("c:\program^files\programb.py") Is there any way I can tell Python to assume the default directory is wherever the interface script is ran from? So say I have a folder and inside are all my python...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.