473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Checking list by using of exception

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
3 1297
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jon Perez | last post by:
I have a C extension function into which I pass a list of lists of tuples: , , , ] I then unpack the values (down to the tuple elements) into their C values using:
5
by: Dylan Parry | last post by:
Hi, I am by no means a Python programmer, but I am dabbling with it and trying to create a simple program that reports how many emails I have to download. So far, using the poplib extension, I...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
0
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
0
by: Anders Borum | last post by:
Hello! I would like to request a new method on the XsltArgumentList class, allowing developers to check the presense of a key/value pair. If you try to add a key/value pair that has already been...
8
by: Brendan | last post by:
There must be an easy way to do this: For classes that contain very simple data tables, I like to do something like this: class Things(Object): def __init__(self, x, y, z): #assert that x,...
10
by: Fredrik Tolf | last post by:
If I have a variable which points to a function, can I check if certain argument list matches what the function wants before or when calling it? Currently, I'm trying to catch a TypeError when...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
16
by: Joe Strout | last post by:
Let me preface this by saying that I think I "get" the concept of duck- typing. However, I still want to sprinkle my code with assertions that, for example, my parameters are what they're...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.