Pythonクロスプラットフォームですべてのファイルを数えながら、大きなディレクトリのサイズをすばやく計算するにはどうすればよいですか?これは私の現在のコードですが、大きなファイル数(100000を超える)では非常に遅くなります:
class filecounter:
def count(self, scandir):
global filescount
global totalsize
if not scandir[-1] == '/' or '\\':
scandir = scandir + '/'
try:
for item in os.listdir(scandir):
if os.path.isdir(scandir + item):
filecounter().count(scandir + item)
else:
totalsize = totalsize + os.path.getsize(scandir +item)
filescount = filescount + 1
except WindowsError, IOError:
pass
グローバルが必要です