Error while using listdir in Python
Asked Answered
G

9

14

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?

Grouchy answered 16/3, 2013 at 17:5 Comment(2)
Just wondering if this has something to do with the '/' in windows ! Ideally, on windows, you should have something like os.path.join('C:','client_side')Flanders
Does this answer your question? Python raising FileNotFoundError for file name returned by os.listdirRitzy
G
3

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.

Grouchy answered 17/3, 2013 at 3:52 Comment(0)
D
16

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)])
Deipnosophist answered 16/3, 2013 at 17:55 Comment(4)
Hi. I don't have any windows error now but it always output the number of files as 0 although I have 7 files in the folder. Do you know why?Grouchy
@Sakura: because os.listdir returns names. You should not use os.path.isfile on those names. I updated my post and gave a full explanation.Deipnosophist
@Grouchy You should really change the accepted answerJacqualinejacquard
For OS operation in general, it would be nicer to do try except instead of if else as there might be other file operation happening at the same time. i.e. try: os.listdir('/client_side/') except os.error: print("error listing directory")Thicken
G
3

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.

Grouchy answered 17/3, 2013 at 3:52 Comment(0)
W
2

Two things:

  1. os.listdir() does not do a glob pattern matching, use the glob module for that
  2. probably you do not have a directory called '/client_side/*.*', but maybe one without the . in the name

The syntax you used works fine, if the directory you look for exists, but there is no directory called '/client_side/.'.

In addition, be careful if using Python 2.x and os.listdir, as the results on windows are different when you use u'/client_side/' and just '/client_side'.

Wotton answered 16/3, 2013 at 17:19 Comment(1)
The glob is added by posix.listdir. In Win32 it uses FindFirstFileW (or the ANSI version for bytes). It doesn't allow a trailing slash. So, instead of special casing root directories such as C:\ , Python just appends *.*.Meteoritics
H
2

You can do just

os.listdir('client_side')

without slashes.

Hindrance answered 16/3, 2013 at 18:34 Comment(1)
Hi. I don't have any windows error now but it always output the number of files as 0 although I have 7 files in the folder. Do you know why?Grouchy
F
1

As I can see a WindowsError, Just wondering if this has something to do with the '/' in windows ! Ideally, on windows, you should have something like os.path.join('C:','client_side')

Flanders answered 16/3, 2013 at 17:16 Comment(0)
G
1

You want:

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

with a "." before "/client_side/".

The dot means the current path where you are working (i.e. from where you are calling your code), so "./client_side/" represents the path you want, which is specified relatively to your current directory.

If you write only "/client_side/", in unix, the program would look for a folder in the root of the system, instead of the folder that you want.

Gunwale answered 16/3, 2013 at 17:19 Comment(0)
E
0

If you just want to see all the files in the directory where your script is located, you can use os.path.dirname(sys.argv[0]). This will give the path of the directory where your script is.

Then, with fnmatch function you can obtain the list of files in that directory with a name and/or extension specified in the filenamevariable.

import os,sys
from fnmatch import fnmatch

directory = os.path.dirname(sys.argv[0])    #this determines the directory
file_name= "*"                              #if you want the python files only, change "*" to "*.py"

for path, subdirs, files in os.walk(directory):
    for name in files:
        if fnmatch(name, file_name):
            print (os.path.join(path, name))

I hope this helps.

Exile answered 5/3, 2019 at 18:3 Comment(0)
P
0

Checking for existence is subject to a race. Better to handle the error (beg forgiveness instead of ask permission). Plus, in Python 3 you can suppress errors. Use suppress from contextlib:

 with suppress(FileNotFoundError):
     for name in os.listdir('foo'):
         print(name)
Philistine answered 6/9, 2019 at 23:25 Comment(0)
A
-1

In case you might be using Pycharm or any type of IDE consider that you could be specifying wrong the working directory.

I hope you have a nice day.

Aphonic answered 10/11, 2023 at 12:46 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Phonetics

© 2022 - 2024 — McMap. All rights reserved.