pythonfiledirectorylistdir

Error while using listdir in Python


I'm trying to get the list of files in a particular directory and count the number of files in the directory. I always get the following error:

WindowsError: [Error 3] The system cannot find the path specified: '/client_side/*.*'

My code is:

print len([name for name in os.listdir('/client_side/') if os.path.isfile(name)])

I followed the code example given here.

I am running the Python script on Pyscripter and the directory /client_side/ do exists. My python code is in the root folder and has a sub-folder called "client_side". Can someone help me out on this?


Solution

  • I decided to change the code into:

    def numOfFiles(path):
        return len(next(os.walk(path))[2])
    

    and use the following the call the code:

    print numOfFiles("client_side")
    

    Many thanks to everyone who told me how to pass the windows directory correctly in Python and to nrao91 in here for providing the function code.