[Tutor] How to get script to detect whether a file exists?
Richard D. Moores
rdmoores at gmail.com
Wed Aug 4 01:37:33 CEST 2010
On Tue, Aug 3, 2010 at 08:04, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, 3 Aug 2010 10:52:27 am Richard D. Moores wrote:
>> On Mon, Aug 2, 2010 at 16:57, Steven D'Aprano <steve at pearwood.info>
> wrote:
>> > # File *probably* doesn't exist. Consider better error checking.
>>
>> Steve, before I dig into your detailed reply, please tell me what you
>> meant by " # File *probably* doesn't exist. Consider better error
>> checking.".
>
> open(pathname) will fail with an IOError for many reasons. It could be
> that the file simply doesn't exist, so it can't be opened. It could be
> that you don't have read permission, or that the disk is corrupt and
> even though you have permission to open the file, attempting to do so
> fails.
>
> For a simple script, it's acceptable to assume that the likely reason
> for IOError is that the file isn't there, and therefore creating it
> will solve the problem. But for a more solid program, you should
> inspect the exception to determine the error code, and then take
> appropriate action.
Ah. Got it. Thanks, Steven.
Dick
More information about the Tutor
mailing list