python 计数文件并显示最大值
python count files and display max
我想统计一个目录中的所有文件范围。然后计算它们并显示其中有多少。做这个的最好方法是什么?此脚本包含所有 pdf= 0
行代码。
另外如何显示有多少文件的输出,从高到低。
import os
pdf = 0
doc = 0
docx = 0
xls = 0
xlsx = 0
ppt = 0
pptx = 0
for file in os.listdir("C:\Users\joey\Desktop\school\ICOMMH"):
if file.endswith(".pdf"):
pdf += 1
print(file)
if file.endswith(".doc"):
doc += 1
print(file)
if file.endswith(".docx"):
docx += 1
print(file)
if file.endswith(".xls"):
xls += 1
print(file)
if file.endswith(".xlsx"):
xlsx += 1
print(file)
if file.endswith(".ppt"):
ppt += 1
print(file)
if file.endswith(".pptx"):
pptx += 1
print(file)
print(pdf)
print(doc)
print(docx)
这是您使用 collections.defaultdict
的地方
from collections import defaultdict
import os.path
d = defaultdict(int)
for fname in os.listdir("C:\Users\joey\Desktop\school\ICOMMH"):
_, ext = os.path.splitext(fname)
d[ext] += 1
然后你会得到一个看起来像这样的字典:
{'.pdf': 7, # or however many...
'.doc': 3,
'.docx': 2, ...}
然后您可以通过以下操作显示最常见的:
max(d, key=lambda k: d[k])
您可以用这些格式替换文件名然后使用 collections.Counter
:
from colections import Counter
import re
print Counter([re.sub(r'.*\.(\w+'),r'',i) for i in os.listdir("C:\Users\joey\Desktop\school\ICOMMH")]
或者正如@Adam Smith 提到的,您可以使用 os.path.splitext(i)[1]
而不是 re.sub
:
print Counter([os.path.splitext(i)[1] for i in os.listdir("C:\Users\joey\Desktop\school\ICOMMH")]
从高到低显示可以使用most_common
方法:
count=Counter([re.sub(r'.*\.(\w+'),r'',i) for i in os.listdir("C:\Users\joey\Desktop\school\ICOMMH")]
for i, j in count.most_common():
print i,j
使用 splitext
和 Counter
及其 most_common
因为你说你想要从高到低。
import os, collections
extensions = (os.path.splitext(f)[1] for f in os.listdir())
for ext, cnt in collections.Counter(extensions).most_common():
print(ext, cnt)
打印例如:
.txt 33
.csv 12
.py 10
.png 8
4
.json 2
.class 1
.c 1
.pl 1
.exe 1
.java 1
.sh 1
你可以使用字典:
import os
exts = {}
my_exts = ('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx')
for file in os.listdir("C:\Users\joey\Desktop\school\ICOMMH"):
ext = os.path.splitext(file)[1]
if ext and ext[1:] in my_exts:
exts[ext] = exts.get(ext, 0) + 1
print sorted(exts.items(), key=lambda x: x[1], reverse=True)
输出将是:
[('.doc', 4), ('.pdf', 2), ('.xlsx', 1)]
在我看来,如果您使用 "glob" 库 (link: https://docs.python.org/2/library/glob.html) 会更容易。这个库的使用很简单,就像你发送一个文件名和扩展名的模式一样简单,它会 return .py 文件的同一目录中匹配该模式的文件列表。
示例:
import glob;
pdf_files_list = glob.glob("*.pdf"); # star "*" represents wild card.
那么您可以通过以下方式知道文件的数量:
len(pdf_files_list); # len is a function that returns the length of a list.
我想统计一个目录中的所有文件范围。然后计算它们并显示其中有多少。做这个的最好方法是什么?此脚本包含所有 pdf= 0
行代码。
另外如何显示有多少文件的输出,从高到低。
import os
pdf = 0
doc = 0
docx = 0
xls = 0
xlsx = 0
ppt = 0
pptx = 0
for file in os.listdir("C:\Users\joey\Desktop\school\ICOMMH"):
if file.endswith(".pdf"):
pdf += 1
print(file)
if file.endswith(".doc"):
doc += 1
print(file)
if file.endswith(".docx"):
docx += 1
print(file)
if file.endswith(".xls"):
xls += 1
print(file)
if file.endswith(".xlsx"):
xlsx += 1
print(file)
if file.endswith(".ppt"):
ppt += 1
print(file)
if file.endswith(".pptx"):
pptx += 1
print(file)
print(pdf)
print(doc)
print(docx)
这是您使用 collections.defaultdict
from collections import defaultdict
import os.path
d = defaultdict(int)
for fname in os.listdir("C:\Users\joey\Desktop\school\ICOMMH"):
_, ext = os.path.splitext(fname)
d[ext] += 1
然后你会得到一个看起来像这样的字典:
{'.pdf': 7, # or however many...
'.doc': 3,
'.docx': 2, ...}
然后您可以通过以下操作显示最常见的:
max(d, key=lambda k: d[k])
您可以用这些格式替换文件名然后使用 collections.Counter
:
from colections import Counter
import re
print Counter([re.sub(r'.*\.(\w+'),r'',i) for i in os.listdir("C:\Users\joey\Desktop\school\ICOMMH")]
或者正如@Adam Smith 提到的,您可以使用 os.path.splitext(i)[1]
而不是 re.sub
:
print Counter([os.path.splitext(i)[1] for i in os.listdir("C:\Users\joey\Desktop\school\ICOMMH")]
从高到低显示可以使用most_common
方法:
count=Counter([re.sub(r'.*\.(\w+'),r'',i) for i in os.listdir("C:\Users\joey\Desktop\school\ICOMMH")]
for i, j in count.most_common():
print i,j
使用 splitext
和 Counter
及其 most_common
因为你说你想要从高到低。
import os, collections
extensions = (os.path.splitext(f)[1] for f in os.listdir())
for ext, cnt in collections.Counter(extensions).most_common():
print(ext, cnt)
打印例如:
.txt 33
.csv 12
.py 10
.png 8
4
.json 2
.class 1
.c 1
.pl 1
.exe 1
.java 1
.sh 1
你可以使用字典:
import os
exts = {}
my_exts = ('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx')
for file in os.listdir("C:\Users\joey\Desktop\school\ICOMMH"):
ext = os.path.splitext(file)[1]
if ext and ext[1:] in my_exts:
exts[ext] = exts.get(ext, 0) + 1
print sorted(exts.items(), key=lambda x: x[1], reverse=True)
输出将是:
[('.doc', 4), ('.pdf', 2), ('.xlsx', 1)]
在我看来,如果您使用 "glob" 库 (link: https://docs.python.org/2/library/glob.html) 会更容易。这个库的使用很简单,就像你发送一个文件名和扩展名的模式一样简单,它会 return .py 文件的同一目录中匹配该模式的文件列表。
示例:
import glob;
pdf_files_list = glob.glob("*.pdf"); # star "*" represents wild card.
那么您可以通过以下方式知道文件的数量:
len(pdf_files_list); # len is a function that returns the length of a list.