I need to count the number of files in a directory using Python. I guess the easiest way is len(glob.glob('*'))
, but that also counts the directory as a file.
我需要使用Python计算一个目录中文件的数量。我想最简单的方法是len(glob.glob('*')),但它也将目录作为一个文件。
Is there any way to count only the files in a directory?
有没有办法只计算目录中的文件?
18 个解决方案
#1
154
os.listdir()
will be slightly more efficient than using glob.glob
. To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile()
:
os.listdir()将比使用glob.glob稍微高效一些。要测试文件名是否为普通文件(而不是目录或其他实体),请使用os.path.isfile():
import os, os.path
# simple version for working with CWD
print len([name for name in os.listdir('.') if os.path.isfile(name)])
# path joining version for other paths
DIR = '/tmp'
print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
#2
52
import os
path, dirs, files = next(os.walk("/usr/lib"))
file_count = len(files)
#3
25
For all kind of files, subdirectories included:
对于所有类型的文件,包括子目录:
import os
list = os.listdir(dir) # dir is your directory path
number_files = len(list)
print number_files
Only files (avoiding subdirectories):
只有文件(避免子目录):
import os
onlyfiles = next(os.walk(dir))[2] #dir is your directory path as string
print len(onlyfiles)
#4
20
This is where fnmatch comes very handy:
这就是fnmatch的用武之地:
import fnmatch
print len(fnmatch.filter(os.listdir(dirpath), '*.txt'))
More details: http://docs.python.org/2/library/fnmatch.html
更多信息:http://docs.python.org/2/library/fnmatch.html。
#5
10
def directory(path,extension):
list_dir = []
list_dir = os.listdir(path)
count = 0
for file in list_dir:
if file.endswith(extension): # eg: '.txt'
count += 1
return count
#6
8
import os
print len(os.listdir(os.getcwd()))
#7
5
This uses os.listdir
and works for any directory:
它使用操作系统。listdir,适用于任何目录:
import os
directory = 'mydirpath'
number_of_files = len([item for item in os.listdir(directory) if os.path.isfile(os.path.join(directory, item))])
this can be simplified with a generator and made a little bit faster with:
这可以用一个发电机简化,并且用:
import os
isfile = os.path.isfile
join = os.path.join
directory = 'mydirpath'
number_of_files = sum(1 for item in os.listdir(directory) if isfile(join(directory, item)))
#8
4
If you want to count all files in the directory - including files in subdirectories, the most pythonic way is:
如果要计算目录中的所有文件——包括子目录中的文件,最符合python的方法是:
import os
file_count = sum(len(files) for _, _, files in os.walk(r'C:\Dropbox'))
print(file_count)
We use sum that is faster than explicitly adding the file counts (timings pending)
我们使用sum,它比显式地添加文件计数(计时待定)要快
#9
3
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
Taked from this post
采取从这篇文章
#10
3
import os
def count_files(in_directory):
joiner= (in_directory + os.path.sep).__add__
return sum(
os.path.isfile(filename)
for filename
in map(joiner, os.listdir(in_directory))
)
>>> count_files("/usr/lib")
1797
>>> len(os.listdir("/usr/lib"))
2049
#11
3
I am surprised that nobody mentioned os.scandir
:
我很惊讶没有人提到os.scandir:
def count_files(dir):
return len([1 for x in list(os.scandir(dir)) if x.is_file()])
#12
2
Luke's code reformat.
卢克的代码重新格式化。
import os
print len(os.walk('/usr/lib').next()[2])
#13
2
Here is a simple one-line command that I found useful:
这里有一个简单的单行命令,我觉得很有用:
print int(os.popen("ls | wc -l").read())
#14
0
import os
total_con=os.listdir('<directory path>')
files=[]
for f_n in total_con:
if os.path.isfile(f_n):
files.append(f_n)
print len(files)
#15
0
If you'll be using the standard shell of the operating system, you can get the result much faster rather than using pure pythonic way.
如果您将使用操作系统的标准shell,您可以更快地得到结果,而不是使用纯python方式。
Example for Windows:
窗口的示例:
import os
import subprocess
def get_num_files(path):
cmd = 'DIR \"%s\" /A-D /B /S | FIND /C /V ""' % path
return int(subprocess.check_output(cmd, shell=True))
#16
0
I found another answer which may be correct as accepted answer.
我找到了另一个可能是正确的答案。
for root, dirs, files in os.walk(input_path):
for name in files:
if os.path.splitext(name)[1] == '.TXT' or os.path.splitext(name)[1] == '.txt':
datafiles.append(os.path.join(root,name))
print len(files)
#17
0
I used glob.iglob
for a directory structure similar to
我用一团。用于类似于的目录结构的iglob
data
└───train
│ └───subfolder1
│ | │ file111.png
│ | │ file112.png
│ | │ ...
│ |
│ └───subfolder2
│ │ file121.png
│ │ file122.png
│ │ ...
└───test
│ file221.png
│ file222.png
Both of the following options return 4 (as expected, i.e. does not count the subfolders themselves)
以下两个选项都返回4(如预期的那样,即不计算子文件夹本身)
len(list(glob.iglob("data/train/*/*.png", recursive=True)))
- len(列表(glob.iglob(“数据/火车/ * / *。png”,递归= True)))
sum(1 for i in glob.iglob("data/train/*/*.png"))
- sum(1 for i in glob.iglob("data/train/*/*.png"))
#18
0
i did this and this returned the number of files in the folder(Attack_Data)...this works fine.
我这样做了,它返回了文件夹(Attack_Data)中的文件数量……这是很好。
import os
def fcount(path):
#Counts the number of files in a directory
count = 0
for f in os.listdir(path):
if os.path.isfile(os.path.join(path, f)):
count += 1
return count
path = r"C:\Users\EE EKORO\Desktop\Attack_Data" #Read files in folder
print (fcount(path))
#1
154
os.listdir()
will be slightly more efficient than using glob.glob
. To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile()
:
os.listdir()将比使用glob.glob稍微高效一些。要测试文件名是否为普通文件(而不是目录或其他实体),请使用os.path.isfile():
import os, os.path
# simple version for working with CWD
print len([name for name in os.listdir('.') if os.path.isfile(name)])
# path joining version for other paths
DIR = '/tmp'
print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
#2
52
import os
path, dirs, files = next(os.walk("/usr/lib"))
file_count = len(files)
#3
25
For all kind of files, subdirectories included:
对于所有类型的文件,包括子目录:
import os
list = os.listdir(dir) # dir is your directory path
number_files = len(list)
print number_files
Only files (avoiding subdirectories):
只有文件(避免子目录):
import os
onlyfiles = next(os.walk(dir))[2] #dir is your directory path as string
print len(onlyfiles)
#4
20
This is where fnmatch comes very handy:
这就是fnmatch的用武之地:
import fnmatch
print len(fnmatch.filter(os.listdir(dirpath), '*.txt'))
More details: http://docs.python.org/2/library/fnmatch.html
更多信息:http://docs.python.org/2/library/fnmatch.html。
#5
10
def directory(path,extension):
list_dir = []
list_dir = os.listdir(path)
count = 0
for file in list_dir:
if file.endswith(extension): # eg: '.txt'
count += 1
return count
#6
8
import os
print len(os.listdir(os.getcwd()))
#7
5
This uses os.listdir
and works for any directory:
它使用操作系统。listdir,适用于任何目录:
import os
directory = 'mydirpath'
number_of_files = len([item for item in os.listdir(directory) if os.path.isfile(os.path.join(directory, item))])
this can be simplified with a generator and made a little bit faster with:
这可以用一个发电机简化,并且用:
import os
isfile = os.path.isfile
join = os.path.join
directory = 'mydirpath'
number_of_files = sum(1 for item in os.listdir(directory) if isfile(join(directory, item)))
#8
4
If you want to count all files in the directory - including files in subdirectories, the most pythonic way is:
如果要计算目录中的所有文件——包括子目录中的文件,最符合python的方法是:
import os
file_count = sum(len(files) for _, _, files in os.walk(r'C:\Dropbox'))
print(file_count)
We use sum that is faster than explicitly adding the file counts (timings pending)
我们使用sum,它比显式地添加文件计数(计时待定)要快
#9
3
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
Taked from this post
采取从这篇文章
#10
3
import os
def count_files(in_directory):
joiner= (in_directory + os.path.sep).__add__
return sum(
os.path.isfile(filename)
for filename
in map(joiner, os.listdir(in_directory))
)
>>> count_files("/usr/lib")
1797
>>> len(os.listdir("/usr/lib"))
2049
#11
3
I am surprised that nobody mentioned os.scandir
:
我很惊讶没有人提到os.scandir:
def count_files(dir):
return len([1 for x in list(os.scandir(dir)) if x.is_file()])
#12
2
Luke's code reformat.
卢克的代码重新格式化。
import os
print len(os.walk('/usr/lib').next()[2])
#13
2
Here is a simple one-line command that I found useful:
这里有一个简单的单行命令,我觉得很有用:
print int(os.popen("ls | wc -l").read())
#14
0
import os
total_con=os.listdir('<directory path>')
files=[]
for f_n in total_con:
if os.path.isfile(f_n):
files.append(f_n)
print len(files)
#15
0
If you'll be using the standard shell of the operating system, you can get the result much faster rather than using pure pythonic way.
如果您将使用操作系统的标准shell,您可以更快地得到结果,而不是使用纯python方式。
Example for Windows:
窗口的示例:
import os
import subprocess
def get_num_files(path):
cmd = 'DIR \"%s\" /A-D /B /S | FIND /C /V ""' % path
return int(subprocess.check_output(cmd, shell=True))
#16
0
I found another answer which may be correct as accepted answer.
我找到了另一个可能是正确的答案。
for root, dirs, files in os.walk(input_path):
for name in files:
if os.path.splitext(name)[1] == '.TXT' or os.path.splitext(name)[1] == '.txt':
datafiles.append(os.path.join(root,name))
print len(files)
#17
0
I used glob.iglob
for a directory structure similar to
我用一团。用于类似于的目录结构的iglob
data
└───train
│ └───subfolder1
│ | │ file111.png
│ | │ file112.png
│ | │ ...
│ |
│ └───subfolder2
│ │ file121.png
│ │ file122.png
│ │ ...
└───test
│ file221.png
│ file222.png
Both of the following options return 4 (as expected, i.e. does not count the subfolders themselves)
以下两个选项都返回4(如预期的那样,即不计算子文件夹本身)
len(list(glob.iglob("data/train/*/*.png", recursive=True)))
- len(列表(glob.iglob(“数据/火车/ * / *。png”,递归= True)))
sum(1 for i in glob.iglob("data/train/*/*.png"))
- sum(1 for i in glob.iglob("data/train/*/*.png"))
#18
0
i did this and this returned the number of files in the folder(Attack_Data)...this works fine.
我这样做了,它返回了文件夹(Attack_Data)中的文件数量……这是很好。
import os
def fcount(path):
#Counts the number of files in a directory
count = 0
for f in os.listdir(path):
if os.path.isfile(os.path.join(path, f)):
count += 1
return count
path = r"C:\Users\EE EKORO\Desktop\Attack_Data" #Read files in folder
print (fcount(path))