[Tutor] if: else: can not get else to work

Marc Tompkins marc.tompkins at gmail.com
Wed Jul 8 03:48:36 CEST 2009


On Tue, Jul 7, 2009 at 6:36 PM, David <david at pythontoo.com> wrote:

> My question is I tried to get it to print out when the directory was empty
> like this;
>
> for file in fobj:
>    pathname = os.path.join(folder, file)
>    if os.path.exists(pathname):
>        print 'removing... ', file
>        os.remove(pathname)
>    else:
>        print 'No files to clean'
>
> But if there are no files in the directory it never gets to the else.
>

It shouldn't, the way you've got it set up...  As I read your program, the
only way it would get to the "else" would be if some other process deleted
one of the files in the directory AFTER your script got a directory listing,
but before your script gets around to deleting it.

-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090707/2746939c/attachment.htm>


More information about the Tutor mailing list