2

我正在尝试制作一个程序来遍历我的 C 驱动器中的每个文件,检查文件的大小并将大小值附加到名为“filesizes_stored”的列表中。

from matplotlib import pyplot as plt
import os
import collections

filesizes_stored = []

for (path, dir, files) in os.walk("c:/"):
    for filename in files:
        print unicode(os.path.join(path, filename), 'cp949')
        # print os.path.getsize(unicode(os.path.join(path, filename)))
        filesizes_stored.append(
            os.path.getsize(
                unicode(
                    os.path.expandvars(
                        os.path.expanduser(
                            os.path.join(path, filename))),'cp949'))/1024.0)

print len(filesizes_stored)

我用 python 2.7 编写了这个脚本,我的电脑操作系统是 Windows 7。

当我执行此脚本时,执行过程顺利进行了一段时间,大多数文件都成功运行,但随后执行停止并出现错误提示

Traceback (most recent call last):
  File "C:/Users/Kim/PycharmProjects/filesize_gatherer/gather_filesize.py", line 15, in <module>
    os.path.expanduser(os.path.join(path, filename))), 'cp949'))/1024.0)
File "C:\Users\Kim\Anaconda2\lib\genericpath.py", line 57, in getsize
    return os.stat(filename).st_size
WindowsError c:/Users\Kim\AppData\Local\Temp\$$$52FE.tmp
: [Error 2] : u'c:/Users\\Kim\\AppData\\Local\\Temp\\$$52FE.tmp'

我怀疑文件名的“$$$”部分是问题的根源。

4

0 回答 0