[GRASS-user] Running an external executable file from a Python Script

2011-03-10 Thread António Rocha

Greetings all,

I have a python script from where I need to run an external binary (in 
Windows). This external binary (not developed by me) uses an 
input_parameter file to configure binary and get other parameters. The 
problem is that this binary requires that in my active folder I have my 
parameter file. Example: If I want to run this binary (outside GRASS 
Python Scripts) i open cmd, go to a folder where I have a parameter file 
(e.g. c:\delete (cd c:\delete)) Then  I can run this binary as long as I 
have my parameter file in my active folder like this 
(c:\tool\training.exe). What I mean is that in my active folder I do not 
need to have my binary only my parameter file.
My question is, when I'm running a GRASS python Script what is my active 
folder in order to place there my Parameter file? Or, is there any way 
to change my active folder while I'm running GRASS python Script?


Hope I was clear enough in this email since this was unexpected for me.

Thanks
Antonio


__ Information from ESET NOD32 Antivirus, version of virus signature 
database 5941 (20110310) __

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Running an external executable file from a Python Script

2011-03-10 Thread Glynn Clements

António Rocha wrote:

 My question is, when I'm running a GRASS python Script what is my active 
 folder in order to place there my Parameter file? Or, is there any way 
 to change my active folder while I'm running GRASS python Script?

By active folder, I presume that you're referring to the current
directory (aka working directory, current working directory or CWD). 
This is inherited from the calling process; e.g. if you run a script
from a shell, the script's current directory will be the shell's
current directory.

When executing a command via subprocess.Popen(), you can specify its
current directory via the cwd= parameter. The grass.Popen() and
grass.call() functions accept this parameter, as do all of the
grass.*_command() functions for running GRASS modules.

You can change the current directory for the current process using
os.chdir(), but that should normally be avoided, as any relative
filenames will then be interpreted relative to the new current
directory, whereas the user probably intended them to be relative to
the initial current directory.

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Re: [GRASS-user] Running an external executable file from a Python Script

2011-03-10 Thread Ricardo Filipe Soares Garcia da
Hi (Olá)

in order to figure out what is your current working folder (or active
folder) you can do

# python code

import os
os.getcwd()

# end of code

This will return a string with your current working folder.
As Glyn is stating, if you are going to call this external binary from
within a python script you can use a grass.Popen object (or just a
normal subprocess.Popen).
The grass.Popen object allows you to capture your external binary's
output and (eventual) error messages.

In the following example, I'm running the 'ls -l' external command,
using the /home/Documents directory as a working directory for the
external command. Please adapt to your problem / operating system:

# python code

import grass.script as grass

externalCommand = [ls, -l] # note that it is a list
externalProcess = grass.Popen(externalCommand, stdout=grass.PIPE,
stderr=grass.PIPE, cwd=/home/ricardo/Documents)
sdtout, stderr = externalProcess.communicate()

# to show the output of your external program
print(stdout)


# end of code


Hope it helps ;)

2011/3/10 Glynn Clements gl...@gclements.plus.com:

 António Rocha wrote:

 My question is, when I'm running a GRASS python Script what is my active
 folder in order to place there my Parameter file? Or, is there any way
 to change my active folder while I'm running GRASS python Script?

 By active folder, I presume that you're referring to the current
 directory (aka working directory, current working directory or CWD).
 This is inherited from the calling process; e.g. if you run a script
 from a shell, the script's current directory will be the shell's
 current directory.

 When executing a command via subprocess.Popen(), you can specify its
 current directory via the cwd= parameter. The grass.Popen() and
 grass.call() functions accept this parameter, as do all of the
 grass.*_command() functions for running GRASS modules.

 You can change the current directory for the current process using
 os.chdir(), but that should normally be avoided, as any relative
 filenames will then be interpreted relative to the new current
 directory, whereas the user probably intended them to be relative to
 the initial current directory.

 --
 Glynn Clements gl...@gclements.plus.com
 ___
 grass-user mailing list
 grass-user@lists.osgeo.org
 http://lists.osgeo.org/mailman/listinfo/grass-user




-- 
___ ___ __
Ricardo Garcia Silva
___
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user