2

I'm trying to make a program which iterates over every file in my C drive, checking the file's size and appending the size value to the list named '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)

I have written this script in python 2.7, and my computer's OS is Windows 7.

When I execute this script, the execution process goes on smoothly for some time, working successfully for most of the files, but then the execution stops with an error saying

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'

I'm suspecting the '$$$' part of the filename to be the source of the problem..

lonelyjoe
  • 115
  • 2
  • 7
  • 2
    I would imagine that the underlying temporary file was deleted between the getting the list of files and then getting details (`stat`) about that file. – AChampion Sep 30 '16 at 22:28

0 Answers0