Checking list by using of exception

N

Nader

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.
 
G

Gabriel Genellina

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.
 
N

Nader

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.

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
 
T

TheSaint

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top