在 Python 中使用 listdir 时出错 [英] Error while using listdir in Python

查看:36
本文介绍了在 Python 中使用 listdir 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取特定目录中的文件列表并计算该目录中的文件数.我总是收到以下错误:

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/*.*'

我的代码是:

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

我遵循了此处的代码示例.

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

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?

推荐答案

我决定把代码改成:

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

并使用以下调用代码:

print numOfFiles("client_side")

非常感谢所有告诉我如何在 Python 中正确传递 windows 目录的人以及在 此处 用于提供函数代码.

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.

感谢 eryksun 更正我的代码!

Thank you eryksun for correcting my code!

这篇关于在 Python 中使用 listdir 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆