2

I'm writing a script that saves an image file to the desktop (so "image.JPG"). After doing that, I want to open the image file so that it is displayed on the screen. I've been trying to use subprocess (which I have no experience with), but can't seem to get it to work.

import subprocess
subprocess.Popen("C:/Users/first.last/Desktop/image.JPG")

However, that results in the following error:

[error] OSError ( Cannot run program "C:/Users/sean.sheehan/Desktop/image.JPG" (in directory "C:\SikuliX"): CreateProcess error=193, %1 is not a valid Win32 application )

I'm assuming this is because it is a file rather than an application (maybe I have to open an application that allows you to view an image?)

So, is there a way to open an image file in sikuli/jython/python without having to double click it with sikuli? I'd also prefer not to download any additional packages.

Thanks.

user2869231
  • 1,431
  • 5
  • 24
  • 53

1 Answers1

1

If it was a normal Python, you would use os.startfile():

Start a file with its associated application.

import os

filename = "C:/Users/first.last/Desktop/image.JPG"
os.startfile(filename)

But, the problem is that the are things (C-based functionality) that exist in Python, but are missing in Jython. And os.startfile() is one of these things.

subprocess.call() should work in Jython:

import subprocess

filename = "C:/Users/first.last/Desktop/image.JPG"
subprocess.call('start ' + filename)

Also see more options at:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Cool. Does that work in Jython, though? It worked using python on the cmd line, but doesn't seem to work in Sikuli (Jython). Either way, I looked into it and using "os.system(filename)" does the trick, so thank you. – user2869231 Dec 26 '14 at 18:15
  • @user2869231 yeah, correct, `os.startfile()` [does not exist in Jython at all](https://answers.launchpad.net/sikuli/+question/185801). – alecxe Dec 26 '14 at 18:29
  • @user2869231 ok, done with edits now, `subprocess.call()` should work, please check. – alecxe Dec 26 '14 at 18:39