在Python中使用listdir时出错

问题描述:

我想获取特定目录中的文件列表并计算目录中的文件数。我总是得到以下错误:在Python中使用listdir时出错

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

我的代码是:

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

我跟着给here的代码示例。

我正在Pyscripter上运行Python脚本,并且存在目录/ client_side/do。我的python代码位于根文件夹中,并有一个名为“client_side”的子文件夹。有人可以帮我解决这个问题吗?

+1

只是想知道这是否与Windows中的'/'有关!理想情况下,在windows上,你应该有'os.path.join'('C:','client_side')' – GodMan 2013-03-16 17:13:50

我决定改变代码为:

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

,并使用以下调用代码:

print numOfFiles("client_side") 

非常感谢大家谁告诉我如何正确地从窗目录Python和用于提供函数代码的here中的nrao91。

编辑:谢谢你eryksun纠正我的代码!

+0

谢谢!我忘了目录部分。我相应地更新了我的代码。 – Sakura 2013-03-17 09:22:16

+1

不客气。 [nymk的答案](http://stackoverflow.com/a/15452633/205580)非常好,我认为你应该接受它作为你的问题的答案。 – eryksun 2013-03-17 09:40:21

正如我所看到的WindowsError,只是想知道这是否与'/'在Windows中有关!理想情况下,在Windows上,你应该有类似os.path.join('C:','client_side')

你想: “”

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

用在“/ client_side /”之前。

点表示您正在工作的当前路径(即从您调用代码的位置开始),因此“./client_side/”表示您希望的路径,它是相对于当前目录指定的路径。

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

两件事情:

  1. os.listdir()不会做的glob模式匹配,使用该
  2. glob模块可能是你没有一个叫“/client_side/*.*目录',但也许一个 没有在名称

你用精品工程的语法,如果您要查找的目录存在,但没有所谓的“/ client_side/目录。'。

此外,使用Python 2.x和os.listdir时要小心,因为在使用u'/ client_side /'和'/ client_side'时,窗口上的结果是不同的。

+0

glob由'posix.listdir'添加。在Win32中它使用['FindFirstFileW'](http://msdn.microsoft.com/en-us/library/aa364418)(或ANSI版本的字节)。它不允许尾随斜线。因此,Python不是像'C:\'这样的特殊的套接字根目录,而是附加'*。*'。 – eryksun 2013-03-17 04:04:54

当您在路径上使用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/*.*' 

如果你想使用os.listdir,你需要或者保证你会使用,或使用os.path.exists首先检查是否存在路径的存在。

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

假设当前的工作目录是c:\foobaros.listdir('/client_side/')相当于os.listdir('c:/client_side'),而os.listdir('client_side/')相当于os.listdir('c:/foobar/client_side')。如果您的client_side目录不在根中,则在使用os.listdir时会发生此类错误。

为了您的0输出中的问题,让我们回顾一下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.

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回报既不是绝对路径,也没有相对路径,但文件名的列表,而isfile需要路径。因此,所有这些名字都会产生False

要获取路径,我们可以使用os.path.join,直接连接两个字符串。

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

或者

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

嗨。我现在没有任何Windows错误,但它总是输出文件数为0,尽管我在文件夹中有7个文件。你知道为什么吗? – Sakura 2013-03-17 03:20:11

+0

@Sakura:因为'os.listdir'返回名字。你不应该在这些名字上使用'os.path.isfile'。我更新了我的帖子并给出了完整的解释。 – nymk 2013-03-17 08:59:31

+0

@Sakura你应该真的改变接受的答案 – Antonio 2016-06-14 20:34:32

你可以做到这

os.listdir('client_side') 

没有斜杠。

+0

嗨。我现在没有任何Windows错误,但它总是输出文件数为0,尽管我在文件夹中有7个文件。你知道为什么吗? – Sakura 2013-03-17 03:22:20