[Tutor] Directory or File ?

Wolfram Kraus kraus at hagen-partner.de
Tue Aug 2 16:27:19 CEST 2005


Damien wrote:
> Hi all,
> I want to know if a file is a directory or a simple file.
> I have done a little ugly script :
> # i is my filename, os is imported
> old = os.getcwd()
> try:
>     chdir(i)
>     is_dir = True
> except:
>     is_dir = False
> os.chdir(old)
> return is_dir
> 
> But maybe there is a better way ?
> If you know a better way, please tell me.
> 
> Thanks,
> Damien G.

Use os.path.isdir:
http://python.org/doc/2.4.1/lib/module-os.path.html#l2h-1739

HTH,
Wolfram



More information about the Tutor mailing list