Dan*_*ach 241
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))])
Run Code Online (Sandbox Code Playgroud)
Luk*_*uke 84
import os
path, dirs, files = next(os.walk("/usr/lib"))
file_count = len(files)
Run Code Online (Sandbox Code Playgroud)
Gui*_*ira 41
对于所有类型的文件,子目录包括:
import os
list = os.listdir(dir) # dir is your directory path
number_files = len(list)
print number_files
Run Code Online (Sandbox Code Playgroud)
只有文件(避免子目录):
import os
onlyfiles = next(os.walk(dir))[2] #dir is your directory path as string
print len(onlyfiles)
Run Code Online (Sandbox Code Playgroud)
nge*_*eek 30
这就是fnmatch非常方便的地方:
import fnmatch
print len(fnmatch.filter(os.listdir(dirpath), '*.txt'))
Run Code Online (Sandbox Code Playgroud)
更多细节:http://docs.python.org/2/library/fnmatch.html
小智 23
简短而简单
import os
directory_path = '/home/xyz/'
No_of_files = len(os.listdir(directory_path))
Run Code Online (Sandbox Code Playgroud)
ras*_*ash 12
import os
print len(os.listdir(os.getcwd()))
Run Code Online (Sandbox Code Playgroud)
小智 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
Run Code Online (Sandbox Code Playgroud)
如果要计算目录中的所有文件 - 包括子目录中的文件,最pythonic方式是:
import os
file_count = sum(len(files) for _, _, files in os.walk(r'C:\Dropbox'))
print(file_count)
Run Code Online (Sandbox Code Playgroud)
我们使用比显式添加文件计数更快的总和(正在等待的时间)
没有人提到我很惊讶os.scandir
:
def count_files(dir):
return len([1 for x in list(os.scandir(dir)) if x.is_file()])
Run Code Online (Sandbox Code Playgroud)
虽然我同意@DanielStutzbach 提供的答案:os.listdir()
将比使用glob.glob
.
但是,一个额外的精度,如果你想计算文件夹中特定文件的数量,你想使用len(glob.glob())
. 例如,如果您要计算要使用的文件夹中的所有 pdf:
pdfCounter = len(glob.glob1(myPath,"*.pdf"))
Run Code Online (Sandbox Code Playgroud)
这os.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))])
Run Code Online (Sandbox Code Playgroud)
使用发电机可以简化这一过程,并且可以通过以下方式加快:
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)))
Run Code Online (Sandbox Code Playgroud)
使用 pathlib 并且不将整个列表加载到内存中的答案:
from pathlib import Path
path = Path('.')
print(sum(1 for _ in path.glob('*'))) # Files and folders, not recursive
print(sum(1 for _ in path.glob('**/*'))) # Files and folders, recursive
print(sum(1 for x in path.glob('*') if x.is_file())) # Only files, not recursive
print(sum(1 for x in path.glob('**/*') if x.is_file())) # Only files, recursive
Run Code Online (Sandbox Code Playgroud)
这是一个简单的解决方案,可以计算包含子文件夹的目录中的文件数量。它可能会派上用场:
import os
from pathlib import Path
def count_files(rootdir):
'''counts the number of files in each subfolder in a directory'''
for path in pathlib.Path(rootdir).iterdir():
if path.is_dir():
print("There are " + str(len([name for name in os.listdir(path) \
if os.path.isfile(os.path.join(path, name))])) + " files in " + \
str(path.name))
count_files(data_dir) # data_dir is the directory you want files counted.
Run Code Online (Sandbox Code Playgroud)
您应该得到与此类似的输出(当然,占位符已更改):
There are {number of files} files in {name of sub-folder1}
There are {number of files} files in {name of sub-folder2}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
取自这篇文章
一行和递归:
def count_files(path):
return sum([len(files) for _, _, files in os.walk(path)])
count_files('path/to/dir')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
286416 次 |
最近记录: |