2017-10-10 57 views
-1

我正在计算不同目录中每个文件扩展名所占用的空间以及文件扩展名和它们的数量。 我能取回所有文件扩展名和它们的计数由下面的代码:计算python中每个文件扩展名消耗的内存

import os 
def count_all_ext (path): 
    res = {} 
    for root,dirs,files in os.walk(path): 
     for f in files : 
      if '.' in f : 
       e = f.rsplit('.',1)[1] 
       res[e] = res.setdefault(e,0)+1 
    return res.items() 
op= '\n'.join('%s:%d'%i for i in count_all_ext('/home')) 

以下的输出是:

baseB:4 
code:100 
cache-6:55 
Xauthority:1 
baseA:4 
npmrc:1 
apmrc:1 
gz:186 
dbf:1 
lst:2 
markdown:10 
sqlite-shm:2 
vsixmanifest:4 
ttf:109 
pkl:31 
gitignore:8 
xml:46 
22:1 
la:9 
sublime-keymap:1 
cache:103 
jar:1 
ts:704 
desktop:3 
source:1 
sqlite3-journal:1 
TAG:1 
4:1 
usage:6 
oTN4k5:1 
bin:6 
docx:9 
rb:1 
woff:3 
db:17 
gpg-agent:1 
V2XO5Y:1 
dat:3 
fingerprint:4 
lz4:1 
converted-launchers:1 
bat:5 
bau:1 
pset:42 
name:2 
crt:8 
dll:42 
h:122 
cmd:2 
list:2 
xlb:2 
dic:1 
zsh-update:1 
editorconfig:4 
stderr:8 
sublime_session:1 
xls:1 
bak:1 
bond:108 
node:16 
56:3 
old:26 
babelrc:2 
locale:1 
cfg:3 
htm:1 
odt:1 
keyring:1 
gypi:1 
meta:2 
md~:1 
pma:2 
sqlite-journal:1 
odb:2 
patch:2 
todo:1 
tvc:1 
out:20 
tmp:4 
ps1:8 
stats:6 
icc:1 
zprofile:1 
dbt:1 
re:21 
3:1 
mozlz4:2 
7:9 
ics:3 
spec:9 
sys:1 
reg:3 
metadata:6 
dirs:1 
whl:2 
run:1 
asar:1 
jsonlz4:14 
tdb:4 
journal:2 
zshrc:1 
gradle:3 
little:2 
pub:1 
js:4336 
asc:1 
git:1 
key:12 
xbel:1 
properties:2 
bash:1 
eslintignore:1 
c:283 
zip:6 
idx:5 
lsup7I:1 
zcompdump:1 
un~:2 
vbs:1 
base:1 
dmrc:1 
Mdg80A:1 
sbstore:42 
pdf:18 
tmLanguage:11 
xlc:2 
py:483 
tgz:2 
gitkeep:2 
iml:2 
xz:1 
bashrc:1 
db-journal:3 
pf2:5 
localstorage:95 
yaml:8 
tmPreferences:4 
isrunning:1 
txt:287 
orig:2 
gvdb:1 
xpi:9 
php:12 
gitmodules:1 
log:49 
swo:1 
jshintrc:6 
stamp:2 
vxd:1 
fmt:1 
1vCfy0:1 
sock:2 
pb:32 
gif:3 
json:2128 
2:2 
js-20170612122310:1 
prl:9 
swp:2 
bash_logout:1 
final:1 
pl:1 
exe:21 
a:2 
sdv:1 
x86_64-pc-linux-gnu:1 
parentlock:2 
cson:8 
rcache:2 
6:1 
otf:29 
sublime-settings:1 
xinputrc:1 
gitconfig:1 
npmignore:84 
localstorage-journal:95 
gyp:1 
rst:11 
update-timestamp:1 
bnf:1 
png:2340 
db-shm:1 
info:1 
md:844 
js-20170816210634:1 
sublime-snippet:14 
tsv:1 
gpg:1 
sth:19 
mk:5 
yml:47 
sqlite-wal:2 
ino:1 
pem:4 
deb:3 
zsh:235 
pack:5 
zsh_history:1 
sqlite:24 
tar:1 
stdout:8 
jscsrc:1 
lock:3 
pro:1 
DB:4 
coffee:8 
jpg:23 
jamignore:1 
sample:72 
watchr:1 
jshintignore:1 
ini:14 
conf:17 
xcu:1 
sudo_as_admin_successful:1 
plist:1 
xsession-errors:1 
keystore:1 
nls:1 
bdic:1 
0:11 
1:8 
html:89 
5:10 
MZPZ5Y:1 
sqlite3:1 
pak:61 
config:1 
css:112 

所以我想在输出形式:

file extension:<totalsize consumed> 
. 
. 
. 

如何通过操纵上述代码找到每个扩展所使用的文件扩展名和空间。 在此先感谢!

+0

我投票作为题外话,因为它是一个工作请求 –

+0

@ivan_pozdeev我能够关闭这个问题

import os def count_all_ext (path): res = {} for root,dirs,files in os.walk(path): for f in files : if '.' in f : statinfo = os.stat(os.path.join(root,f)) e = f.rsplit('.',1)[1] res.setdefault(e,[]).append((statinfo.st_size)) return res.items() op= '\n'.join('{}:{}'.format(key, array) for key,array in count_all_ext(r'C:\Users\user\.anaconda\navigator')) print(op) 

参考资料检索特定扩展名的值,但无法为所有扩展名实现相同的效果!!因此寻求新手帮助 – Sjn73

+0

尝试用'os.stat(path).st_size'替换文件扩展名“+ 1”。阅读/浏览['os'](https://docs.python.org/3.3/library/os.html)和['os.path'](https://docs.python.org/3.3/ library/os.path.html)模块页面,所以你可以学习'os.path.splitext(path)'这样的方法。 – Harvey

回答

3

我建议看看os.stat,这个方法将允许你读取文件大小以字节为单位。如果您希望以兆字节为单位的其他大小,则需要在两者之间进行一些转换。

这里是什么让你开始:为os.stat

+0

好的答案!此外,请使用os.path.splitext而不是f.rsplit –

+0

@ Sjn73 sum(数组)将在您打印该值时起作用,或者在追踪文件时保持运行总和 – will7200

+0

@WilliamFlores:yeah used sum()method , 谢谢您的帮助! – Sjn73