Forum

Author Topic: [SOLVED] Problem with command os.system ?  (Read 20511 times)

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Problem with command os.system ?
« on: December 02, 2016, 07:10:05 PM »
Dear all,

I'm stucked with a trouble in a script.
When I put the command "os.system" within the script, it does nothing.
When I do the exact same command within the console, it works.

Is there any explanation to this behaviour ?

Many thanks in advance,

Denis
« Last Edit: December 06, 2016, 05:57:12 PM by denis_f »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Problem with command os.system ?
« Reply #1 on: December 02, 2016, 07:25:58 PM »
Hello Denis,

Can you provide any example of the command that you are using?
Best regards,
Alexey Pasumansky,
Agisoft LLC

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with command os.system ?
« Reply #2 on: December 05, 2016, 11:24:26 AM »
Спасибо болшой for the quick answer Alexey !

In fact we want to do a R post-processing called from the Photoscan Python script.
A colleague of mine started from your script taken in this topic and we then looped. The scripts works quite good, thanks for that.

Here is our command :
command="/usr/bin/Rscript /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/Create_polygons_footprints.R "+save_path

Where
save_path = PhotoScan.app.getSaveFileName("Specify output file:")

The strange behaviour is the following :

When I put
os.system(command)
within the script, I don't see nothing, whereas when I copy/paste the command within the Photoscan Python console, it works ... ? Any idea ?

Many thanks in advance,

Denis

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Problem with command os.system ?
« Reply #3 on: December 05, 2016, 12:05:04 PM »
Hello Denis,

Have you tried printing command and save_path variables via print to the Console?
getSaveFileName() returns the full path to the file, not only filename.
Best regards,
Alexey Pasumansky,
Agisoft LLC

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with command os.system ?
« Reply #4 on: December 05, 2016, 01:40:09 PM »
Dear Alexey,

Indeed, my current workaround is exactly what you propose : I print the command in the console and I then copy-paste so that it does the job. Here are the final lines of our script :

command="/usr/bin/Rscript /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/Create_polygons_footprints.R "+save_path
os.system(command)
print(" CAUTION - dirty WORKAROUND")
print("If it does not work, copy paste the following command and press enter")
print("os.system('"+command+"')")

Please notice that when printing the desired command I had to add quotes so that the printed string is correct.

Denis

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Problem with command os.system ?
« Reply #5 on: December 05, 2016, 04:15:59 PM »
Hello Denis,

Can you do the following: assign a new variable, as follows
Code: [Select]
status = os.system(command)and then print it
Code: [Select]
print(status)
Please provide the Console pane output and output from the terminal (where from you are stating PhotoScan) related to the script run (if it's a sensible data, you can send it to support@agisoft.com). I think we need both outputs - from the script and from the Console directly.

Please also specify, just to clarify, if the command works fine when you are executing the script lines one by one in the Console pane?
Best regards,
Alexey Pasumansky,
Agisoft LLC

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with command os.system ?
« Reply #6 on: December 06, 2016, 11:37:49 AM »
Many thanks Aleksey for your quick answers.

Here is the code :
Code: [Select]
print("part with os command - BEGIN")
command="/usr/bin/Rscript /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/Create_polygons_footprints.R "+save_path
print(" CAUTION - dirty WORKAROUND")
print("If it does not work, copy paste the following command and press enter")
print("os.system('"+command+"')")
status=os.system(command)
print(status)
print("part with os command - END")

And the result in the console
Code: [Select]
2016-12-06 09:27:38 part with os command - BEGIN
2016-12-06 09:27:38  CAUTION - dirty WORKAROUND
2016-12-06 09:27:38 If it does not work, copy paste the following command and press enter
2016-12-06 09:27:38 os.system('/usr/bin/Rscript /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/Create_polygons_footprints.R /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/out.txt')
2016-12-06 09:27:38 256
2016-12-06 09:27:38 part with os command - END

Unfortunately I've had already tried to look at the exit status of the system command. If I'm not wring, 256 equals to 1, which means that the exit is ok ?

Another thing I tried this morning : I gave 777 rights (chmod a+rwx . -R) to the working directory. Same behaviour. I'm puzzled.

Many thanks again !

Denis

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Problem with command os.system ?
« Reply #7 on: December 06, 2016, 12:46:07 PM »
Hello Denis,

I think that the following article on Stackoverflow should answer the question about 256 return code:
http://stackoverflow.com/questions/23788969/python-sys-exit-return-code

But could you please specify, if the same command works when you run it from PhotoScan Console pane manually, and what's the output? Also is it different if you run the command from the system Python?
« Last Edit: December 06, 2016, 12:57:02 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with command os.system ?
« Reply #8 on: December 06, 2016, 01:13:08 PM »
Hi again Alexey,

I re-ran the script and then did the tests you advised. I copy paste here what's in the photoscan console :
Code: [Select]
2016-12-06 11:01:20 part with os command - END
2016-12-06 11:01:20 Script finished in 21.45 seconds.
>>> import os
>>> statusconsole=os.system('/usr/bin/Rscript /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/Create_polygons_footprints.R /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/out.txt')
>>> print(statusconsole)
2016-12-06 11:02:09 0
>>>
I get my out.shp out.dbf and out.shx shapefile files, newly created by the R script

Here is the same in a system python console :
Code: [Select]
>>> import os
>>> statusconsolepython=os.system('/usr/bin/Rscript /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/Create_polygons_footprints.R /home/feurer/0WX_en_cours/20161202_Fabrice_export_footprints_pscan/out.txt')
Loading required package: methods
Checking rgeos availability: TRUE
>>> print(statusconsolepython)
0
>>>
I also get my out.shp out.dbf and out.shx shapefile files, newly created by the R script

Hence if I understand well :
1. direct execution of os.system command in a Python console or in the Photoscan console gives a zero exit status, which indicates that everything is ok (which is the case, my shapefiles are actually created)
2. execution of the os.system command within a python script called from the menu Tools>Run Script... gives a non-zero status (1) which indicates that something went wrong (which is the case : no shapefiles actually created).

The question is now : why do the command work in the console and not when running a script ?

Denis


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Problem with command os.system ?
« Reply #9 on: December 06, 2016, 05:14:00 PM »
Hello Denis,

Can you try to execute following small script from the Console pane and from Run Script dialog and compare the outputs:
Code: [Select]
import os
print(os.system("echo 239"))
Is it the same, or in the second case the script also fails?
Best regards,
Alexey Pasumansky,
Agisoft LLC

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Problem with command os.system ?
« Reply #10 on: December 06, 2016, 05:56:55 PM »
Hello Alexey,

Thanks to your helpful assistance ! Following your questions, I finally found were the trouble came from !! Urrrah !

The trouble was that my R script was trying to work on a file opened for writing within the python script and that the file was not closed before the R script os.sytem call. When putting the command outside the script, the file was closed, which explained that the trouble did not show up.
I simply put the "file.close()" command before calling the R script with os.system => that works !

Many thanks for your very helpful support.

Denis

P.S : by the way, I got 0 status from both tries, which guided me to test which specific context was explaining the fact that my os.system call failed.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: [SOLVED] Problem with command os.system ?
« Reply #11 on: December 06, 2016, 05:58:36 PM »
Glad to hear, that it finally worked and there are no more issues.
Best regards,
Alexey Pasumansky,
Agisoft LLC

denis_f

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: [SOLVED] Problem with command os.system ?
« Reply #12 on: December 06, 2016, 06:00:34 PM »
Many thanks again, Alexey, in particular for your impressive reactivity !

Cheers,

Denis