0

I create function that calculate sum of files size in side folder. in advance i can exclude file extension

def sourceStatic(path, exclude):

    # exclude list convert to lower 
    exclude = list(map(lambda x:x.lower(), exclude))
    files_size = 0
    files_count = 0
    for root, dirs, files in os.walk(path):
        for fileName in files:
            fname, fileEx = os.path.splitext(fileName)
            fileEx = (fileEx[1:]).lower()
            if not any(fileEx in item for item in exclude):
                print(fileName)
                filePath = os.path.join(root,fileName)
                fileSize = getsize(filePath)
                files_size += fileSize
                files_count += 1

    # return multiple data as dictionary              
    ret = {}
    ret['files_size'] = size(files_size)
    ret['files_size_byte'] = files_size
    ret['files_count'] = files_count
    print(ret)
    return (ret) 

above method return file count/size in folder. os.walk(path) go through every files and sub-folders

problem is once found file path more than 260 character getsize(filepath) give error as bellow

  File "C:\Python34\lib\genericpath.py", line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [WinError 3] The system cannot find the path specified:

so what is best way to calculate files size. is there any other method/lib?

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
user881703
  • 1,111
  • 3
  • 19
  • 38

1 Answers1

1

It's due to the limitations of WinAPI on windows platform. Any file path which exceeds 260 character have to treated in special way. Read this python win32 filename length workaround

filePath = u"\\\\?\\" + filePath
fileSize = getsize(filePath)
....
Community
  • 1
  • 1
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97