计算目录中的文件 [英] count files in a directory

查看:68
本文介绍了计算目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为有一种比这更好的方法来递归计算

目录中的文件。有没有???


def count_em(valid_path):

x = 0

for root,dirs,os中的文件。 walk(valid_path):

for f in files:

x = x + 1

print"有,x,文件在这个目录中。

返回x


rbt

解决方案

在2005年5月20日星期五07:12 pm,rbt写道:

我认为有一个比这更好的方法来递归计算
目录中的文件。是否有

def count_em(valid_path):
x = 0
对于root,dirs,os.walk中的文件(valid_path):
for f在文件中:
x = x + 1
打印此目录中有,x,文件。
返回x

rbt




def count_em(valid_path):

root,dirs,files = os.walk(valid_path)

返回len(文件)


-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

Box 951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/




来想一想


file_count = len(os.walk(valid_path)[2])


-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/

James Stroud写道:

来想一想

file_count = len(os.walk(valid_path)[2])

-
James Stroud
加州大学洛杉矶分校基因组学和蛋白质组学研究所
Box 951570
洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/



Somee可能的答案是:


#文件直接在路径中

file_count = len(os.walk(path)[2])


#文件和dirs直接在路径中

file_count = len(os.listdir(路径))


#路径中或路径下的文件

file_count = 0

for root,dirs,os.walk中的文件(路径):

file_count + = len (文件)

#文件和dirs在路径中或路径下

file_count = 0

for ro ot,dirs,os.walk(路径)中的文件:

file_count + = len(文件)+ len(dirs)

--Scott David Daniels
Sc***********@Acm.Org


I assume that there''s a better way than this to count the files in a
directory recursively. Is there???

def count_em(valid_path):
x = 0
for root, dirs, files in os.walk(valid_path):
for f in files:
x = x+1
print "There are", x, "files in this directory."
return x

rbt

解决方案

On Friday 20 May 2005 07:12 pm, rbt wrote:

I assume that there''s a better way than this to count the files in a
directory recursively. Is there???

def count_em(valid_path):
x = 0
for root, dirs, files in os.walk(valid_path):
for f in files:
x = x+1
print "There are", x, "files in this directory."
return x

rbt



def count_em(valid_path):
root, dirs, files = os.walk(valid_path)
return len(files)

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



Come to think of it

file_count = len(os.walk(valid_path)[2])

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/


James Stroud wrote:

Come to think of it

file_count = len(os.walk(valid_path)[2])

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



Somee possible answers are:

# files directly in path
file_count = len(os.walk(path)[2])

# files and dirs directly in path
file_count = len(os.listdir(path))

# files in or below path
file_count = 0
for root, dirs, files in os.walk(path):
file_count += len(files)
# files and dirs in or below path
file_count = 0
for root, dirs, files in os.walk(path):
file_count += len(files) + len(dirs)
--Scott David Daniels
Sc***********@Acm.Org


这篇关于计算目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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