I am trying to write a piece of code to tell me how big a directory is so I can see if I want to delete it. I ran across the error in the title when I was testing this piece of code.
import os
class File_Manager:
def __init__(self):
pass
def find_size(self, path):
assert type(path) == str, "Path should be a string"
size = 0
if os.path.isdir(path):
for root, dirs, files in os.walk(path, topdown=False):
for file in files:
size += os.stat(f"{root}\{file}", follow_symlinks=False).st_size
size = self.convert_size(size)
elif os.path.isfile(path):
stats = os.stat(path)
size = self.convert_size(stats.st_size)
return size
def convert_size(self, num):
size_ext = {0:"B", 1:"KB", 2:"MB", 3:"GB", 4:"TB"}
ext_num = 0
while num > 1024:
num /= 1024
ext_num += 1
return f"{int(num)}{size_ext[ext_num]}"
manager = File_Manager()
a = manager.find_size("C:\\")
print(a)
Expected Output:
[somesize] GB
Actual Output:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Windows\\servicing\\LCU\\Package_for_RollupFix~31bf3856ad364e35~amd64~~18362.836.1.6\\amd64_microsoft-windows-a..g-whatsnew.appxmain_31bf3856ad364e35_10.0.18362.752_none_cf994d2ae256d6d5\\f\\new360videossquare44x44logo.targetsize-16_altform-unplated_contrast-black.png'
When I follow the path in the file explorer it is there. When I open the properties tab I get this Location: C:\Windows\servicing\LCU\PACKAG~1.6\AM2317~1.752\f" and this as the File: new360videossquare44x44logo.targetsize-16_altform-unplated_contrast-black.png
which when joined doesn't look like the path in the error. os.stat
works with the path in the error all the way up until I append the filename
Ex. os.stat('C:\\Windows\\servicing\\LCU\\Package_for_RollupFix~31bf3856ad364e35~amd64~~18362.836.1.6\\amd64_microsoft-windows-a..g-whatsnew.appxmain_31bf3856ad364e35_10.0.18362.752_none_cf994d2ae256d6d5\\f')
works. os.expanduser
doesn't help me either. Just throwing that in there because I read something about needing to expand the user directory. Also file on disk is 0, I read that meant it was a resident file. I don't know if any of that matters, but that's what I found out trying to resolve this issue. I was wondering if someone could tell me why I get this error if the file exists and if you could help me resolve this error.