在Python中使用listdir时出错

时间:2022-01-29 15:23:56

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?

我正在运行Pyscripter和目录/client_side/ do的Python脚本。我的python代码位于根文件夹中,有一个名为“client_side”的子文件夹。有人能帮我一下吗?

6 个解决方案

#1


12  

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

这个错误发生在您使用os时。不引用现有路径的路径上的listdir。例如:

>>> 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.

如果你想使用os。您需要保证要使用的路径的存在,或者使用os.path。存在是为了先验证存在。

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.

假设您当前的工作目录是c:\foobar, os.listdir('/client_side/')与os.listdir('c:/client_side')等价,而os.listdir('c:/foobar/client_side')等价于os.listdir('c:/foobar/client_side')。如果您的client_side目录不在根目录中,那么在使用os.listdir时将发生这种错误。

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

对于0输出问题,让我们回忆一下os.listdir(路径)

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).

和os.path.isfile(路径)。

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.

如果路径是一个现有的常规文件,则返回True。这遵循符号链接,因此对于相同的路径,islink()和isfile()都可以为真。

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.

listdir不返回绝对路径或相对路径,而是文件名称的列表,而isfile则需要路径。因此,所有这些名称都会产生False。

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

为了获得路径,我们可以使用os.path。连接,直接连接两个字符串。

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)])

#2


2  

You can do just

你能做的只是

os.listdir('client_side')

without slashes.

没有斜杠。

#3


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')

我看到了一个WindowsError,我想知道这是否与windows中的'/'有关!理想情况下,在windows上,应该有类似os.path.join('C:','client_side')的东西

#4


1  

You want:

你想要的:

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

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

使用"." 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.

点表示您正在工作的当前路径(即从您正在调用代码的位置),so。/client_side/"表示您想要的路径,它是相对于当前目录指定的。

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.

如果您只在unix中编写“/client_side/”,那么程序将在系统的根目录中查找一个文件夹,而不是您想要的文件夹。

#5


1  

Two things:

两件事:

  1. os.listdir() does not do a glob pattern matching, use the glob module for that
  2. os.listdir()不执行glob模式匹配,为此使用glob模块
  3. probably you do not have a directory called '/client_side/*.*', but maybe one without the . in the name
  4. 可能您没有一个名为'/client_side/*的目录。*',但是可能没有。的名字

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

如果要查找的目录存在,但是没有名为“/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'.

此外,如果使用Python 2,请小心。x和操作系统。当你使用u'/client_side/'和just '/client_side'时,在windows上的结果是不同的。

#6


1  

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.

非常感谢所有告诉我如何在Python中正确传递windows目录的人,感谢这里的nrao91提供函数代码。

EDIT: Thank you eryksun for correcting my code!

编辑:谢谢您纠正我的代码!

#1


12  

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

这个错误发生在您使用os时。不引用现有路径的路径上的listdir。例如:

>>> 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.

如果你想使用os。您需要保证要使用的路径的存在,或者使用os.path。存在是为了先验证存在。

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.

假设您当前的工作目录是c:\foobar, os.listdir('/client_side/')与os.listdir('c:/client_side')等价,而os.listdir('c:/foobar/client_side')等价于os.listdir('c:/foobar/client_side')。如果您的client_side目录不在根目录中,那么在使用os.listdir时将发生这种错误。

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

对于0输出问题,让我们回忆一下os.listdir(路径)

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).

和os.path.isfile(路径)。

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.

如果路径是一个现有的常规文件,则返回True。这遵循符号链接,因此对于相同的路径,islink()和isfile()都可以为真。

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.

listdir不返回绝对路径或相对路径,而是文件名称的列表,而isfile则需要路径。因此,所有这些名称都会产生False。

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

为了获得路径,我们可以使用os.path。连接,直接连接两个字符串。

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)])

#2


2  

You can do just

你能做的只是

os.listdir('client_side')

without slashes.

没有斜杠。

#3


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')

我看到了一个WindowsError,我想知道这是否与windows中的'/'有关!理想情况下,在windows上,应该有类似os.path.join('C:','client_side')的东西

#4


1  

You want:

你想要的:

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

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

使用"." 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.

点表示您正在工作的当前路径(即从您正在调用代码的位置),so。/client_side/"表示您想要的路径,它是相对于当前目录指定的。

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.

如果您只在unix中编写“/client_side/”,那么程序将在系统的根目录中查找一个文件夹,而不是您想要的文件夹。

#5


1  

Two things:

两件事:

  1. os.listdir() does not do a glob pattern matching, use the glob module for that
  2. os.listdir()不执行glob模式匹配,为此使用glob模块
  3. probably you do not have a directory called '/client_side/*.*', but maybe one without the . in the name
  4. 可能您没有一个名为'/client_side/*的目录。*',但是可能没有。的名字

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

如果要查找的目录存在,但是没有名为“/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'.

此外,如果使用Python 2,请小心。x和操作系统。当你使用u'/client_side/'和just '/client_side'时,在windows上的结果是不同的。

#6


1  

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.

非常感谢所有告诉我如何在Python中正确传递windows目录的人,感谢这里的nrao91提供函数代码。

EDIT: Thank you eryksun for correcting my code!

编辑:谢谢您纠正我的代码!