Python Forum

Full Version: Failing to get Stat for a Directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Jul-20-2021, 09:40 PM)tester_V Wrote: [ -> ]Sorry for the confusion!
If a directory has a file or 2 or 100 files but each file has a size=0, the directory size is also=0.

That depends on the filesystem. You cannot take that as a rule. Try it on another filesystem (even on the same machine) and that expectation may be broken.

Here's a directory on my machine (appears to be an xfs filesystem) with nothing but 3 empty files. But the "size" of the directory is not zero.

Output:
$ ls -lns total 0 0 -rw-r--r-- 1 29324 101 0 Jul 20 14:56 a 0 -rw-r--r-- 1 29324 101 0 Jul 20 14:56 b 0 -rw-r--r-- 1 29324 101 0 Jul 20 14:56 c $ python3 -c 'from pathlib import Path; print(Path(".").stat().st_size)' 33
Quote:I'm not looking if a directory has file/files or not because if it has a file and filesize=0 it is an "Empty" directory.

It is an empty file. To call it an empty directory is against common usage. An empty directory has no members and rmdir() will succeed on it. Your directory does not match that description.

Quote:I'm looking if a directory size is =0. I thought I could do that with 'pathil.Path'.

You can do that with Path, but the meaning of such directories depends on knowing the specific filesystem you are dealing with. As such it is not recommended. It will fail when you do not expect. Don't do it that way.

I don't have access to a NTFS filesystem at the moment. Your code does exactly what I would expect on my filesystem.

Output:
Directory -> empty Size -> 6 Not Empty Dir ->6 Directory -> full Size -> 15 Not Empty Dir ->15 Directory -> half Size -> 15 Not Empty Dir ->15
OK, now i understand what it is your trying to do.
Pages: 1 2