Checking list by using of exception

Discussion in 'Python' started by Nader, Jun 13, 2008.

  1. Nader

    Nader Guest

    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.
     
    Nader, Jun 13, 2008
    #1
    1. Advertising

  2. 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
     
    Gabriel Genellina, Jun 13, 2008
    #2
    1. Advertising

  3. Nader

    Nader Guest

    On Jun 13, 11:34 am, "Gabriel Genellina" <>
    wrote:
    > 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


    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
     
    Nader, Jun 13, 2008
    #3
  4. Nader

    TheSaint Guest

    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
     
    TheSaint, Jun 14, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. benben
    Replies:
    16
    Views:
    610
    Frank Puck
    Jul 31, 2005
  2. Rehceb Rotkiv

    Checking whether list element exists

    Rehceb Rotkiv, Apr 7, 2007, in forum: Python
    Replies:
    7
    Views:
    363
    Rehceb Rotkiv
    Apr 7, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    708
    Debajit Adhikary
    Oct 18, 2007
  4. vimal
    Replies:
    3
    Views:
    379
    vimal
    Dec 17, 2007
  5. Replies:
    20
    Views:
    594
Loading...

Share This Page