Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47483 > unrolled thread

[newbie] problem with if then

Started byJean Dubois <jeandubois314@gmail.com>
First post2013-06-09 12:44 -0700
Last post2013-06-09 22:37 +0200
Articles 10 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  [newbie] problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 12:44 -0700
    Re: [newbie] problem with if then Fábio Santos <fabiosantosart@gmail.com> - 2013-06-09 21:00 +0100
      Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 13:15 -0700
        Re: problem with if then Fábio Santos <fabiosantosart@gmail.com> - 2013-06-09 21:29 +0100
          Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 13:51 -0700
    Re: [newbie] problem with if then Roy Smith <roy@panix.com> - 2013-06-09 16:23 -0400
      Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-09 14:00 -0700
        Re: problem with if then Roy Smith <roy@panix.com> - 2013-06-09 17:35 -0400
          Re: problem with if then Jean Dubois <jeandubois314@gmail.com> - 2013-06-10 00:46 -0700
    Re: [newbie] problem with if then Albert Dengg <albert@fsfe.org> - 2013-06-09 22:37 +0200

#47483 — [newbie] problem with if then

FromJean Dubois <jeandubois314@gmail.com>
Date2013-06-09 12:44 -0700
Subject[newbie] problem with if then
Message-ID<cd1be83a-f560-4024-90b3-697a51bb1bb0@g7g2000vbv.googlegroups.com>
I'm writing some code to check whether an url is available or not,
therefore I make use of a wget-command in Linux and then check whether
this is successful (returning a 0) or not returning an 8
However the if then statement seems to give the same result in both
cases:
Here is my code:

#!/usr/bin/env python
import sys
import os
from datetime import datetime, timedelta
today=datetime.now()
yesterday= datetime.now() - timedelta(days=1)
daybeforeyesterday= datetime.now() - timedelta(days=2)
collection = [daybeforeyesterday,yesterday,today]
for thisday in collection:
     checkavailablestring='wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisday.strftime("%y%m%d")+'_JO7
>/dev/null ; echo $?'
     if os.system(checkavailablestring)==0:
          print thisday, 'stream is available'
     else:
          print thisday, 'stream is not available'

Can anyone here tell me what I'm doing wrong.
Thanks in advance
jean

[toc] | [next] | [standalone]


#47485

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-06-09 21:00 +0100
Message-ID<mailman.2932.1370808058.3114.python-list@python.org>
In reply to#47483

[Multipart message — attachments visible in raw view] — view raw

On 9 Jun 2013 20:49, "Jean Dubois" <jeandubois314@gmail.com> wrote:
>
> I'm writing some code to check whether an url is available or not,
> therefore I make use of a wget-command in Linux and then check whether
> this is successful (returning a 0) or not returning an 8
> However the if then statement seems to give the same result in both
> cases:

Which result? Failure, or success? Have you tried printing the
checkavailablestring string and running that command exactly? There may be
something wrong with the command or URL in some way, and calling os.system
instead of using the shell directly will hide any errors from you.

[toc] | [prev] | [next] | [standalone]


#47488 — Re: problem with if then

FromJean Dubois <jeandubois314@gmail.com>
Date2013-06-09 13:15 -0700
SubjectRe: problem with if then
Message-ID<2cb97d50-87b1-4173-a4d9-0cb4342bfd5f@g2g2000vbk.googlegroups.com>
In reply to#47485
On 9 jun, 22:00, Fábio Santos <fabiosantos...@gmail.com> wrote:
> On 9 Jun 2013 20:49, "Jean Dubois" <jeandubois...@gmail.com> wrote:
>
>
>
> > I'm writing some code to check whether an url is available or not,
> > therefore I make use of a wget-command in Linux and then check whether
> > this is successful (returning a 0) or not returning an 8
> > However the if then statement seems to give the same result in both
> > cases:
>
> Which result? Failure, or success?
In case of failure I expect it to answer: stream not available
In case of success I expect it to answer: stream is available
But I get a "stream is available" in both cases even though the
command os.system(checkavailablestring) does give different answers: 0
and 8
>Have you tried printing the
> checkavailablestring string and running that command exactly?
yes I did, no problem there
>There may be something wrong with the command or URL in some way, and calling os.system
I have added a line to the script and the results below so you can see
better what's going wrong:

Here's the script once again, this time with an extra line:

#!/usr/bin/env python
import sys
import os
from datetime import datetime, timedelta
today=datetime.now()
yesterday= datetime.now() - timedelta(days=1)
daybeforeyesterday= datetime.now() - timedelta(days=2)
collection = [daybeforeyesterday,yesterday,today]
for thisday in collection:
     checkavailablestring='wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisday.strftime("%y%m%d")+'_JO7
>/dev/null ; echo $?'
     print checkavailablestring
     if os.system(checkavailablestring)==0:
          print thisday, 'stream is available'
     else:
          print thisday, 'stream is not available'

And here is the result:

jean@antec4:~$ ./try.py
wget -q -O - http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130607_JO7
>/dev/null ; echo $?
8
2013-06-07 22:07:00.016807 stream is available
wget -q -O - http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130608_JO7
>/dev/null ; echo $?
8
2013-06-08 22:07:00.016795 stream is available
wget -q -O - http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130609_JO7
>/dev/null ; echo $?
0
2013-06-09 22:07:00.016763 stream is available















[toc] | [prev] | [next] | [standalone]


#47490 — Re: problem with if then

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-06-09 21:29 +0100
SubjectRe: problem with if then
Message-ID<mailman.2935.1370809789.3114.python-list@python.org>
In reply to#47488

[Multipart message — attachments visible in raw view] — view raw

On 9 Jun 2013 21:24, "Jean Dubois"
...
> And here is the result:
>
> jean@antec4:~$ ./try.py
> wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130607_JO7
> >/dev/null ; echo $?
> 8
> 2013-06-07 22:07:00.016807 stream is available
> wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130608_JO7
> >/dev/null ; echo $?
> 8
> 2013-06-08 22:07:00.016795 stream is available
> wget -q -O -
http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_130609_JO7
> >/dev/null ; echo $?
> 0
> 2013-06-09 22:07:00.016763 stream is available
>

You don't need to echo the return code. os.system should return that as an
int.

The problem might be that os.system is returning the result from the echo
command instead.

[toc] | [prev] | [next] | [standalone]


#47495 — Re: problem with if then

FromJean Dubois <jeandubois314@gmail.com>
Date2013-06-09 13:51 -0700
SubjectRe: problem with if then
Message-ID<a31dbfb2-5563-4db2-a2ea-da8116bd1db2@bz1g2000vbb.googlegroups.com>
In reply to#47490
On 9 jun, 22:29, Fábio Santos <fabiosantos...@gmail.com> wrote:
> On 9 Jun 2013 21:24, "Jean Dubois"
> ...> And here is the result:
>
> > jean@antec4:~$ ./try.py
> > wget -q -O -
>
> http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/E...> >/dev/null ; echo $?
> > 8
> > 2013-06-07 22:07:00.016807 stream is available
> > wget -q -O -
>
> http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/E...> >/dev/null ; echo $?
> > 8
> > 2013-06-08 22:07:00.016795 stream is available
> > wget -q -O -
>
> http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/E...
>
> > >/dev/null ; echo $?
> > 0
> > 2013-06-09 22:07:00.016763 stream is available
>
> You don't need to echo the return code. os.system should return that as an
> int.
>
> The problem might be that os.system is returning the result from the echo
> command instead.

I removed the 'echo $?' and now the thing runs like expected, thanks a
lot

jean

[toc] | [prev] | [next] | [standalone]


#47489

FromRoy Smith <roy@panix.com>
Date2013-06-09 16:23 -0400
Message-ID<roy-DD0DA2.16235509062013@news.panix.com>
In reply to#47483
In article 
<cd1be83a-f560-4024-90b3-697a51bb1bb0@g7g2000vbv.googlegroups.com>,
 Jean Dubois <jeandubois314@gmail.com> wrote:

> I'm writing some code to check whether an url is available or not,
> therefore I make use of a wget-command in Linux and then check whether
> this is successful

In general, "shelling out" to run a command-line utility should be the 
last resort.  It's slower, and more complicated, than doing it in pure 
python.  You would only call a shell command if there was no other way.

Fortunately, in Python, there is another way.  Several, in fact.

The most straight-forward is to use the built-in urllib2 module 
(http://docs.python.org/2/library/urllib2.html).

If you're going to be doing anything complicated (i.e. setting optional 
headers, managing cookies, etc), you probably want to be looking at the 
excellent third-party module, requests (http://python-requests.org).

In any case, given your code:

> #!/usr/bin/env python
> import sys
> import os
> from datetime import datetime, timedelta
> today=datetime.now()
> yesterday= datetime.now() - timedelta(days=1)
> daybeforeyesterday= datetime.now() - timedelta(days=2)
> collection = [daybeforeyesterday,yesterday,today]
> for thisday in collection:
>      checkavailablestring='wget -q -O -
> http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisd
> ay.strftime("%y%m%d")+'_JO7
> >/dev/null ; echo $?'
>      if os.system(checkavailablestring)==0:
>           print thisday, 'stream is available'
>      else:
>           print thisday, 'stream is not available'

I would break the debugging down into several parts.  First, are you 
generating the command string properly?  Try printing out 
checkavailablestring before you call os.system() to make sure it's what 
you think it is.

Next, once you're sure you've got the correct string, run it manually in 
the shell and see what it does.

Next, are you sure you're using os.system() correctly?  Try running:

os.system("/bin/true")

and

os.system("/bin/false")

and make sure you get the results you think you should.  But, really, 
once you've done all that (and it's worth doing as an exercise), rewrite 
your code to use urllib2 or requests.  It'll be a lot easier.

[toc] | [prev] | [next] | [standalone]


#47496 — Re: problem with if then

FromJean Dubois <jeandubois314@gmail.com>
Date2013-06-09 14:00 -0700
SubjectRe: problem with if then
Message-ID<20165c85-4cc3-4b79-943b-82443e4a9bd0@w7g2000vbw.googlegroups.com>
In reply to#47489
On 9 jun, 22:23, Roy Smith <r...@panix.com> wrote:
> In article
> <cd1be83a-f560-4024-90b3-697a51bb1...@g7g2000vbv.googlegroups.com>,
>  Jean Dubois <jeandubois...@gmail.com> wrote:
>
> > I'm writing some code to check whether an url is available or not,
> > therefore I make use of a wget-command in Linux and then check whether
> > this is successful
>
> In general, "shelling out" to run a command-line utility should be the
> last resort.  It's slower, and more complicated, than doing it in pure
> python.  You would only call a shell command if there was no other way.
>
> Fortunately, in Python, there is another way.  Several, in fact.
>
> The most straight-forward is to use the built-in urllib2 module
> (http://docs.python.org/2/library/urllib2.html).
>
> If you're going to be doing anything complicated (i.e. setting optional
> headers, managing cookies, etc), you probably want to be looking at the
> excellent third-party module, requests (http://python-requests.org).
>
> In any case, given your code:
>
>
>
>
>
>
>
>
>
> > #!/usr/bin/env python
> > import sys
> > import os
> > from datetime import datetime, timedelta
> > today=datetime.now()
> > yesterday= datetime.now() - timedelta(days=1)
> > daybeforeyesterday= datetime.now() - timedelta(days=2)
> > collection = [daybeforeyesterday,yesterday,today]
> > for thisday in collection:
> >      checkavailablestring='wget -q -O -
> >http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/E...
> > ay.strftime("%y%m%d")+'_JO7
> > >/dev/null ; echo $?'
> >      if os.system(checkavailablestring)==0:
> >           print thisday, 'stream is available'
> >      else:
> >           print thisday, 'stream is not available'
>
> I would break the debugging down into several parts.  First, are you
> generating the command string properly?  Try printing out
> checkavailablestring before you call os.system() to make sure it's what
> you think it is.
>
> Next, once you're sure you've got the correct string, run it manually in
> the shell and see what it does.
>
> Next, are you sure you're using os.system() correctly?  Try running:
>
> os.system("/bin/true")
>
> and
>
> os.system("/bin/false")
>
> and make sure you get the results you think you should.  But, really,
> once you've done all that (and it's worth doing as an exercise), rewrite
> your code to use urllib2 or requests.  It'll be a lot easier.

Could you show me how to code the  example in metacode below wuth the
use of urllib2?
#!/usr/bin/env python
import urllib2
if check whether url exists succeed:
    print 'url exists'
else:
    print 'url does not exist'

thanks in advance
jean

[toc] | [prev] | [next] | [standalone]


#47499 — Re: problem with if then

FromRoy Smith <roy@panix.com>
Date2013-06-09 17:35 -0400
SubjectRe: problem with if then
Message-ID<roy-03039C.17352009062013@news.panix.com>
In reply to#47496
In article 
<20165c85-4cc3-4b79-943b-82443e4a9bd0@w7g2000vbw.googlegroups.com>,
 Jean Dubois <jeandubois314@gmail.com> wrote:

> But, really,
> > once you've done all that (and it's worth doing as an exercise), rewrite
> > your code to use urllib2 or requests.  It'll be a lot easier.
> 
> Could you show me how to code the  example in metacode below wuth the
> use of urllib2?
> #!/usr/bin/env python
> import urllib2
> if check whether url exists succeed:
>     print 'url exists'
> else:
>     print 'url does not exist'

There are two basic ways Python function return status information.  
Either they return something which indicates failure (such as None), or 
they raise an exception.

In this case, what you want is urlopen(), which is documented at 
http://docs.python.org/2/library/urllib2.html.  The key piece of 
information is a couple of paragraphs down, where it says, "Raises 
URLError on errors".

It's not obvious from reading that whether URLError is a built-in or 
not, but that's easy enough to figure out:

>>> URLError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'URLError' is not defined
>>> import urllib2
>>> urllib2.URLError
<class 'urllib2.URLError'>

So, that means you want something like:

#!/usr/bin/env python

import urllib2

url = "http://whatever...."
try:
   urllib2.urlopen(url)
   print "url exists"
except urllib2.URLError:
   print "url does not exist"

[toc] | [prev] | [next] | [standalone]


#47525 — Re: problem with if then

FromJean Dubois <jeandubois314@gmail.com>
Date2013-06-10 00:46 -0700
SubjectRe: problem with if then
Message-ID<d1f71018-198c-4e5d-9f06-197d48bd25c7@x20g2000vbe.googlegroups.com>
In reply to#47499
On 9 jun, 23:35, Roy Smith <r...@panix.com> wrote:
> In article
> <20165c85-4cc3-4b79-943b-82443e4a9...@w7g2000vbw.googlegroups.com>,
>  Jean Dubois <jeandubois...@gmail.com> wrote:
>
> > But, really,
> > > once you've done all that (and it's worth doing as an exercise), rewrite
> > > your code to use urllib2 or requests.  It'll be a lot easier.
>
> > Could you show me how to code the  example in metacode below wuth the
> > use of urllib2?
> > #!/usr/bin/env python
> > import urllib2
> > if check whether url exists succeed:
> >     print 'url exists'
> > else:
> >     print 'url does not exist'
>
> There are two basic ways Python function return status information.
> Either they return something which indicates failure (such as None), or
> they raise an exception.
>
> In this case, what you want is urlopen(), which is documented athttp://docs.python.org/2/library/urllib2.html.  The key piece of
> information is a couple of paragraphs down, where it says, "Raises
> URLError on errors".
>
> It's not obvious from reading that whether URLError is a built-in or
> not, but that's easy enough to figure out:
>
> >>> URLError
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'URLError' is not defined>>> import urllib2
> >>> urllib2.URLError
>
> <class 'urllib2.URLError'>
>
> So, that means you want something like:
>
> #!/usr/bin/env python
>
> import urllib2
>
> url = "http://whatever...."
> try:
>    urllib2.urlopen(url)
>    print "url exists"
> except urllib2.URLError:
>    print "url does not exist"

thanks a lot, this works like a charm

jean

[toc] | [prev] | [next] | [standalone]


#47492

FromAlbert Dengg <albert@fsfe.org>
Date2013-06-09 22:37 +0200
Message-ID<mailman.2937.1370810586.3114.python-list@python.org>
In reply to#47483
Jean Dubois <jeandubois314@gmail.com> wrote:
...
>     checkavailablestring='wget -q -O -
>http://www.deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_'+thisday.strftime("%y%m%d")+'_JO7
>>/dev/null ; echo $?'
The problem schould be the echo:
Since os.system returns the exit code of the shell, when chaining commands with ; it returns the exit status of the last command,in your case the echo.
So,if you really want to go with wget here,
Either drop the echo or chain with &&

Yours
Albert
Hi,
While i agree that calling wget here is not optimal....

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web