Convert a python non-tail recursive function adding sub-folders info to a loop

I wrote a function to get the total size and total files/subdirs of the entire disk or specific folder. The recursive function was very simple:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Make the python dirSize function a non recursive one:
from pathlib import Path
obj = {}
def dirSize(dir: Path, size = 0, files = 0, dirs = 0):
for sub_dir in dir.glob('*'):
if sub_dir.is_dir():
dirs += 1
res_size, res_files, res_dirs = dirSize(sub_dir)
size += res_size
files += res_files
dirs += res_dirs
elif sub_dir.is_file():
files += 1
size += sub_dir.stat().st_size
obj[dir.as_posix()] = {
'size': size,
'files': files,
'dirs': dirs,
}
return size, files, dirs
dirSize(Path('~/Documents').expanduser())
#Line above is for tests. Final code should run in elevated cmd:
#dirSize(Path('C:/'))
with open(Path('~/Desktop/results.tsv').expanduser().as_posix(), 'w') as file:
file.write('DirectorytBytestFilestDirsn')
for key in sorted(obj):
dir = obj[key]
file.write(key)
file.write('t')
file.write(str(dir['size']))
file.write('t')
file.write(str(dir['files']))
file.write('t')
file.write(str(dir['dirs']))
file.write('n')
</code>
<code># Make the python dirSize function a non recursive one: from pathlib import Path obj = {} def dirSize(dir: Path, size = 0, files = 0, dirs = 0): for sub_dir in dir.glob('*'): if sub_dir.is_dir(): dirs += 1 res_size, res_files, res_dirs = dirSize(sub_dir) size += res_size files += res_files dirs += res_dirs elif sub_dir.is_file(): files += 1 size += sub_dir.stat().st_size obj[dir.as_posix()] = { 'size': size, 'files': files, 'dirs': dirs, } return size, files, dirs dirSize(Path('~/Documents').expanduser()) #Line above is for tests. Final code should run in elevated cmd: #dirSize(Path('C:/')) with open(Path('~/Desktop/results.tsv').expanduser().as_posix(), 'w') as file: file.write('DirectorytBytestFilestDirsn') for key in sorted(obj): dir = obj[key] file.write(key) file.write('t') file.write(str(dir['size'])) file.write('t') file.write(str(dir['files'])) file.write('t') file.write(str(dir['dirs'])) file.write('n') </code>
# Make the python dirSize function a non recursive one:
from pathlib import Path

obj = {}
def dirSize(dir: Path, size = 0, files = 0, dirs = 0):
    for sub_dir in dir.glob('*'):
        if sub_dir.is_dir():
            dirs += 1
            res_size, res_files, res_dirs = dirSize(sub_dir)
            size += res_size
            files += res_files
            dirs += res_dirs
        elif sub_dir.is_file():
            files += 1
            size += sub_dir.stat().st_size
    
    obj[dir.as_posix()] = {
        'size': size,
        'files': files,
        'dirs': dirs,
    }
    
    return size, files, dirs

dirSize(Path('~/Documents').expanduser())
#Line above is for tests. Final code should run in elevated cmd:
#dirSize(Path('C:/'))

with open(Path('~/Desktop/results.tsv').expanduser().as_posix(), 'w') as file:
    file.write('DirectorytBytestFilestDirsn')
    for key in sorted(obj):
        dir = obj[key]
        file.write(key)
        file.write('t')
        file.write(str(dir['size']))
        file.write('t')
        file.write(str(dir['files']))
        file.write('t')
        file.write(str(dir['dirs']))
        file.write('n')

I tried to use AI to convert it to a non recursive function, but AI seems to ignore these lines are a bottom up sum, not a simple sum:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>size += res_size
files += res_files
dirs += res_dirs
</code>
<code>size += res_size files += res_files dirs += res_dirs </code>
size += res_size
files += res_files
dirs += res_dirs

So I tried a stack loop, like in the Dijkstra algorithm, but it turns up that I couldn’t find a simple solution to make the bottom up sum.

Alternatively I could add a level/parent/child properties to the obj variable to make a bottom up sum after the queue processing. But this seems very cumbersome. Is there a simpler way to achieve this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật