Py3: Terminal or browser output?
clp2 at rebertia.com
Sat Feb 13 21:16:20 CET 2010
On Sat, Feb 13, 2010 at 11:46 AM, Gnarlodious <gnarlodious at gmail.com> wrote:
> Hello, searched all over but no success. I want to have a script
> output HTML if run in a browser and plain text if run in a Terminal.
> In Python 2, I just said this:
> if len(sys.argv)==True:
That line doesn't make sense really as it is practically equivalent to:
if len(sys.argv) == 1:
Which, since argv always contains at least 1 element (the program's
name), is essentially checking whether /no/ arguments were passed on
the command line.
Recall that issubclass(bool, int) is true and that True == 1 in Python
due to details of said subclassing.
Note also that "== True" is almost always unnecessary as it is
essentially implicit in the "if".
I suspect you instead meant to write:
if len(sys.argv) > 0:
which for the record can be written more idiomatically as:
if sys.argv: # bool(some_list) is True if some_list is not empty
Which, however, as I explained, is always true since argv is /never/
completely empty, and thus the test is useless. What you probably
if len(sys.argv) > 1:
Which is effectively the opposite of my very first code snippet and
checks whether we /were/ passed some arguments on the command line.
> and it seemed to work.
By happy accident I suspect, due to the aforementioned way == works
between bools and ints and probably a logic error in your code.
> Py3 must have broken that by sending a list
> with the path to the script in BOTH the browser and Terminal. Is there
> some newfangled way to determine what is running the script (hopefully
> without a try wrapper)?
How exactly are you running the script *"in"* the browser? Browsers
via a webserver?
If so, there are /several/ possible ways of doing that, please explain
exactly which you are using.
More information about the Python-list