By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Checking list by using of exception

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
En Fri, 13 Jun 2008 04:37:44 -0300, Nader <n.*****@gmail.comescribió:
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

Jun 27 '08 #2

P: n/a
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.comescribió:
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
Jun 27 '08 #3

P: n/a
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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.