Help Need In Coding

Laurent Pierron Laurent.Pierron at loria.fr
Thu Oct 18 04:39:32 EDT 2001


"Alec D. Cheah" a écrit :

> How do verified from input the variable entered is a file or a path
> and how do you check whether it exist or not??
>
> now let say ... i just specified input a value "xxx.java" i just want
> to check whether it exist in a current directory or subdirectory of
> current directory depending on how many directory level you have. if
> not a file then check whether it a directory????
>
> In this case it will do a searching base on the input value.
>

import os
def existe(file,dir='.'):
    if file in os.listdir(dir): return dir
    for f in os.listdir(dir):
        path = os.path.join(dir,f)
        if os.path.isdir(path) and not os.path.islink(path):
            res = existe(file,path)
            if res: return res
    return None

>>> existe('Percolator.py')
'.\\Tools\\idle'
>>> existe('README.txt')
'.'
>>> existe('toto')
>>>



--
Laurent PIERRON





More information about the Python-list mailing list