1

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.

  • cd into that directory and run the interactive python3 interpreter. run stat on just the filename. Does it work? – Michael Bianconi Jun 19 '20 at 16:36
  • It does not. I tried `os.listdir(os.getcwd())` and it list it as a file but `os.stat` does not work on it. – Corey Smith Jun 19 '20 at 16:47
  • 1
    That path is 265 characters long, which exceeds the classic `MAX_PATH - 1` (259) character limit for DOS paths. Use an extended (verbatim) path such as `"\\\\?\\C:\\"` in order to access the full length that's supported by the filesystem -- about 32,760 characters. Alternatively, use Windows 10 with Python 3.6+ and [enable long DOS paths](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#enable-long-paths-in-windows-10-version-1607-and-later) in the registry. – Eryk Sun Jun 19 '20 at 22:32
  • 2
    An extended path does not get normalized when opened. It must use backslash as the path separator and should not contain "." and ".." components. Also, UNC paths have to use the "UNC" device, e.g. rewrite `"//server/share/spam/../file"` as `"\\\\?\\UNC\\server\\share\\file"`. Before converting to extended form, normalize the path manually with `os.path.abspath`. If the normalized result already starts with `"\\\\?\\"`, there's nothing to do. If it starts with `"\\\\.\\"`, replace the "." with "?". If it's UNC, replace the leading `"\\\\"` with `"\\\\?\\UNC\\"`. Else prepend `"\\\\?\\"`. – Eryk Sun Jun 19 '20 at 22:43
  • I tried to use `os.path.normpath` and `os.path.abspath` neither worked. I added a try-catch block and printed out the paths that had a `FileNotFoundError` and that folder had a ton. The LCU Dir is the only Dir with `FileNotFoundError`. I looked up more about that folder and was told that it was for Windows updates and that it could be deleted if you didn't plan on rolling back your update. – Corey Smith Jun 21 '20 at 18:35
  • Thi should work: `os.stat(r'\\?\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')` – Eryk Sun Jun 22 '20 at 03:39
  • @ErykSun : I ran into this same `FileNotFoundError: [WinError 3]` while I was optimising a Python 3 script, which searched for all directories below a certain size limit (< 1 MB), on a given source Drive. I was using the `pathlib` module to define my paths as `dir_path = Path('D:\TEST_MAIN')`. After reading your comment, I simply modified my path as - `dir_path = Path('\\\\?\\D:\\TEST_MAIN')` and tried again. It worked like a charm! Thank you for your valuable comment! – Amar Aug 21 '20 at 21:29
  • 1
    @Amar, pathlib normalizes Windows paths to use backslash, so you can use `Path('//?/D:/TEST_MAIN')` without having to worry about escaping backslashes. – Eryk Sun Aug 21 '20 at 22:02

0 Answers0