By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

Determine whether program was started by clicking icon or commandline

P: n/a
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
Nov 29 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 29, 9:51 am, Benjamin Hell <bh...@spamfence.netwrote:
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
Nov 29 '07 #2

P: n/a
On Nov 28, 10:51 pm, Benjamin Hell <bh...@spamfence.netwrote:
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.

Nov 29 '07 #3

P: n/a
Roger Miller wrote:
>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'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
Nov 29 '07 #4

P: n/a
Roger Miller wrote:
On Nov 28, 10:51 pm, Benjamin Hell <bh...@spamfence.netwrote:
>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.
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
Nov 30 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.