Determine whether program was started by clicking icon or commandline

B

Benjamin Hell

Hi!

I wonder whether there might be a way to find out how a Python
program was started (in my case in Windows): By double clicking the
file or by calling it on the "DOS" command line prompt.

Background: I would like to have the program run in an "interactive
mode" if double clicked, and silently in a "batch mode" when started
otherwise.

Any hints?

Thank you!

Ben
 
A

Atila Olah

Hi!

I wonder whether there might be a way to find out how a Python
program was started (in my case in Windows): By double clicking the
file or by calling it on the "DOS" command line prompt.

I think it's not possible (or very tricky) to do that.
Background: I would like to have the program run in an "interactive
mode" if double clicked, and silently in a "batch mode" when started
otherwise.

Any hints?

Why don't you just create a desktop icon that calls "myprog.py --
interactive-mode" and then check in sys.argv for the actual argument?
Or, if you prefer not to use arguments, you could just change the
working directory of the shortcut and then check os.getcwd()? Though
it's a weird thing to do...

Hope I could.
aatiis
 
R

Roger Miller

Hi!

I wonder whether there might be a way to find out how a Python
program was started (in my case in Windows): By double clicking the
file or by calling it on the "DOS" command line prompt.

Background: I would like to have the program run in an "interactive
mode" if double clicked, and silently in a "batch mode" when started
otherwise.

I'm not sure whether this applies to your situation, but often
programs
started by clicking an icon are run by pythonw, but when started from
the command line are run by python. If this is the case
sys.stdin.fileno()
will return -1 in the former case and 0 in the latter.
 
B

Benjamin Hell

Roger said:
I'm not sure whether this applies to your situation, but often
programs started by clicking an icon are run by pythonw, but when
started from the command line are run by python. If this is the
case sys.stdin.fileno() will return -1 in the former case and 0
in the latter.

Nice idea, but this...

import sys
print sys.stdin.fileno()
raw_input("Press any key to exit")

....
always prints "0" in my case ("DOS", double click in Windows
Explorer, Cygwin shell).

Thanks anyways!

Ben
 
A

Adonis Vargas

Roger said:
I'm not sure whether this applies to your situation, but often
programs
started by clicking an icon are run by pythonw, but when started from
the command line are run by python. If this is the case
sys.stdin.fileno()
will return -1 in the former case and 0 in the latter.

No, when it gets executed by pythonw the application has an extension of
..pyw and .py files are run by the regular python executable. This the
default behavior, unless for some odd reason you modified this through
the registry or through explorer to do otherwise.

Adonis Vargas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top