免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4421 | 回复: 3
打印 上一主题 下一主题

读取文件大小问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-01 11:12 |只看该作者 |倒序浏览
  1 #!/usr/bin/env python
  2 import os
  3 import string
  4 file0="c:/chrome/Default/Cache"
  5 file1="c:/chrome/Default/Cache/"
  6 n=0
  7 for f in os.listdir(file0):
  8         while (n<10):
  9                 if(os.stat(string.join(file1,f)).st_size>3072):
10                         print "%s---\n" % (f)
11                         n+=1

执行后下是这个
Traceback (most recent call last):
  File "cache.py", line 9, in <module>
    if(os.stat(string.join(file1,f)).st_size>3072):
OSError: [Errno 2] No such file or directory:

论坛徽章:
0
2 [报告]
发表于 2012-08-01 12:40 |只看该作者
你的join命令使用有问题,见join的help:
join(words, sep=' ')
    join(list [,sep]) -> string

    Return a string composed of the words in list, with
    intervening occurrences of sep.  The default separator is a
    single space.

    (joinfields and join are synonymous)


其实你只需用“+”即可,
  1. if(os.stat(file1+f).st_size>3072):
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-08-01 14:53 |只看该作者
  1. 7 for f in os.listdir(file0):
  2.   8         while (n<10):
  3.   9                 if(os.stat(string.join(file1,f)).st_size>3072):
  4. 10                         print "%s---\n" % (f)
  5. 11                         n+=1
复制代码
这样居然都不死循环吗?

论坛徽章:
0
4 [报告]
发表于 2012-08-01 15:19 |只看该作者
回复 3# ilikeqdi

该程序问题是比较多,贴一个我写的改进版:
  1. #!/usr/bin/env python
  2. import os
  3. file0="./"

  4. n = 0
  5. size = 3072
  6. for f in os.listdir(file0):
  7.         if(os.stat(os.path.join(file0,f)).st_size > size):
  8.                 print "%s---" % (f)
  9.                 n +=1
  10.         if (n>=10): break
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP