[Tutor] How to handle try and except in this case

Dave Angel d at davea.name
Tue Nov 29 16:25:09 CET 2011


(You top-posted again.  Try to put your remarks AFTER the part you're 
quoting, so that the message is self-descriptive)

On 11/29/2011 10:16 AM, Mic wrote:
>> Could you explain what's unclear about it? Andreas couldn't get more
>> specific, since you didn't say how these 10 names are provided.
>
>
> Yes, it was probably my fault. Sorry about that. I have come up with a
> new way of working around
> a problem in my main program so I only need two files to be tested if
> they exist instead of say 10 files.
>
> So, the program should check if two text files exist. If neither of them
> exist, and only if neither of them,
> then the program should print say "hi".
>
> If only one of them exist, the program should continue to run without
> crashing.
>
>
> I hope this was more easy to understand!
>

The os.path.exists(filename) returns a boolean, and doesn't tie up 
resources, so you are welcome to use it in an if expression.

if not (os.path.exists(file1) and os.path.exists(file2)):
      print "hi"


-- 

DaveA


More information about the Tutor mailing list