Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Checking list by using of exception

Reply
Thread Tools

Checking list by using of exception

 
 
Nader
Guest
Posts: n/a
 
      06-13-2008
Hello,

I read some files name from a directory and then I put these name in a
list. I will check whether it is empty or not, and I would do it with
an exception. With if statement it is very simple:

If list_of_files != "" : # this can be if list_of_files !=
[]:
get the files
elas:
there is no file

But with exception, I can write something as:

try:
list_of_files != []
get the files
except ValueError:
Print " there is no file"

What can the first statement be inside 'try' if I don't want to use if
statement?
Maybe my understandig of exception is enough to got it.

Would somebody explain me about this?

Regards,
Nader


try and except in a dircMaybe this quetion will be simple enough for
you.
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      06-13-2008
En Fri, 13 Jun 2008 04:37:44 -0300, Nader <> escribió:

> Hello,
>
> I read some files name from a directory and then I put these name in a
> list. I will check whether it is empty or not, and I would do it with
> an exception. With if statement it is very simple:
>
> If list_of_files != "" : # this can be if list_of_files !=
> []:
> get the files
> elas:
> there is no file


If it is simple, just do it! Why do you want to make things more
complicated? This would be enough:

if list_of_files:
get_the_files(list_of_files)
else:
print "there is no file"

(a list has a false boolean value when it is empty)

> But with exception, I can write something as:
>
> try:
> list_of_files != []
> get the files
> except ValueError:
> Print " there is no file"
>
> What can the first statement be inside 'try' if I don't want to use if
> statement?


If you insist on using an exception (and assuming list_of_files is
actually a list, not a string or other kind of sequence):

try:
list_of_files[0]
except IndexError:
...no files...

This way you're checking that list_of_files contains at least one element.
But I would not reccomend it.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Nader
Guest
Posts: n/a
 
      06-13-2008
On Jun 13, 11:34 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Fri, 13 Jun 2008 04:37:44 -0300, Nader <n.em...@gmail.com> escribió:
>
> > Hello,

>
> > I read some files name from a directory and then I put these name in a
> > list. I will check whether it is empty or not, and I would do it with
> > an exception. With if statement it is very simple:

>
> > If list_of_files != "" : # this can be if list_of_files !=
> > []:
> > get the files
> > elas:
> > there is no file

>
> If it is simple, just do it! Why do you want to make things more
> complicated? This would be enough:
>
> if list_of_files:
> get_the_files(list_of_files)
> else:
> print "there is no file"
>
> (a list has a false boolean value when it is empty)
>
> > But with exception, I can write something as:

>
> > try:
> > list_of_files != []
> > get the files
> > except ValueError:
> > Print " there is no file"

>
> > What can the first statement be inside 'try' if I don't want to use if
> > statement?

>
> If you insist on using an exception (and assuming list_of_files is
> actually a list, not a string or other kind of sequence):
>
> try:
> list_of_files[0]
> except IndexError:
> ...no files...
>
> This way you're checking that list_of_files contains at least one element.
> But I would not reccomend it.
>
> --
> Gabriel Genellina


I would accept your suggestion in raltion of checking a list whether
it is empty or not with "if" statement. It is more expressive and
clear. But In this case I would learn more about the "try .... except"
exception.

Nader
 
Reply With Quote
 
TheSaint
Guest
Posts: n/a
 
      06-14-2008
On 15:37, venerdì 13 giugno 2008 Nader wrote:

> try:
> list_of_files != []
> get the files
>

For file in list_of_files:
try:
myfile = open(file, 'r')
except (IOError, OSError):
print"Your %s file wasn't open" %file
# here you can do something with your open file as read option
myfile.readlines() # for example
--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Exception checking memory allocation. How? benben C++ 16 07-31-2005 01:31 AM
Re: bizaar exception that isn't really an exception jeff ASP .Net 0 06-24-2003 12:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57