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

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

问题描述

我正在尝试获取特定目录中的文件列表,并对目录中的文件数进行计数。我总是收到以下错误:

  WindowsError:[错误3]系统找不到指定的路径:'/ client_side / *我的代码是:



$ {

pre> print len([os.listdir('/ client_side /')中的名称,如果os.path.isfile(name)])

我按照代码示例给出了这里



我在Pyscripter上运行Python脚本,目录/ client_side / do存在。我的python代码在根文件夹中,并有一个名为client_side的子文件夹。有人可以帮助我吗?

解决方案

我决定将代码更改为:

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

并使用以下代码:

  print numOfFiles(client_side)

非常感谢所有告诉我如何传递Windows目录正确地在Python和nrao91在这里用于提供功能代码。



编辑:谢谢你们改正我的代码!


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?

解决方案

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.

EDIT: Thank you eryksun for correcting my code!

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

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