Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 748
Sakura Avatar asked Mar 16 '13 17:03

Sakura


People also ask

What does Listdir mean in Python?

Python method listdir() returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '. ' and '..' even if they are present in the directory.

Does OS Listdir include folder?

The listdir() method only lists all the top level files and folders inside a directory. If you want to navigate all the lower level files and folders inside your directory and subdirectories, use the walk() method from the OS module.

How do I print a directory list in Python?

Using the glob module The glob module also makes it possible​ to get a list of files or folders in a directory. print(glob. glob('. '))


1 Answers

This error occurs when you use os.listdir on a path which does not refer to an existing path.
For example:

>>> os.listdir('Some directory does not exist')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
WindowsError: [Error 3] : 'Some directory does not exist/*.*'

If you want to use os.listdir, you need to either guarantee the existence of the path that you would use, or use os.path.exists to check the existence first.

if os.path.exists('/client_side/'):
    do something
else:
    do something

Suppose your current working directory is c:\foobar, os.listdir('/client_side/') is equivalent to os.listdir('c:/client_side'), while os.listdir('client_side/') is equivalent to os.listdir('c:/foobar/client_side'). If your client_side directory is not in the root, such error will occur when using os.listdir.

For your 0 ouput problem, let us recall os.listdir(path)

Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.

and os.path.isfile(path).

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

listdir returns neither the absolute paths nor relative paths, but a list of the name of your files, while isfile requires path. Therefore, all of those names would yield False.

To obtain the path, we can either use os.path.join , concat two strings directly.

print ([name for name in os.listdir(path)
        if os.path.isfile(os.path.join(path, name))])

Or

print ([name for name in os.listdir('client_side/')
        if os.path.isfile('client_side/' + name)])
like image 196
nymk Avatar answered Sep 21 '22 15:09

nymk