Verified Input.

Robert Amesz reqhye72zux at
Thu Oct 18 16:37:04 CEST 2001

Rikard Bosnjakovic wrote:

> Alec D. Cheah wrote:
>> How do verified from input the variable entered is a file or a
>> path and how do you check whether it exist or not??
>> Help...
>> Newbie on Python....
> >>> import os
> >>> s = "/tmp/foobar" os.stat(s)
> [snipped]
> I.e., you should enclose the stat() within a try-except in case the
> file does not exist.

That's doing it the hard way. Try

   import os.path

and use

   os.path.exists("your suspected file here")

if that doesn't return 0, use

   os.path.isfile("your suspected file here")

   os.path.isdir("your suspected file here")

To find out if it's a file or a directory. If you do *any* path 
manipulations you'll do yourself a favour by reading the docs on the 
os.path module.

Robert Amesz

More information about the Python-list mailing list