Python Forum
Failing to get Stat for a Directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Failing to get Stat for a Directory (/thread-34324.html)

Pages: 1 2


Failing to get Stat for a Directory - tester_V - Jul-20-2021

Greetings!
I have directories with an empty file or two, I want to identify and delete them.
I thought I could use Path().stat().st_size but the code fails to identify those derectories.
import pathlib
for ef in pathlib.Path('C:\\02').iterdir() :
    if ef.is_dir() :
        print(f" Directory -> {ef}")
        tt = pathlib.Path(ef).stat().st_size
        print(f" Size -> {tt}")
        if tt==0 :
            print(f" Empty Dir -> {tt}")       
        else :
            print(f"Not Empty Dir ->{tt}")        
Thank you.


RE: Failing to get Stat for a Directory - Larz60+ - Jul-20-2021

try this
from pathlib import Path

# set starting directory
path = Path("C:\\02")

dirs = [x for x in path.iterdir() if x.is_dir()]
for dir in dirs:
    fcount = len([x for x in dir.iterdir() if x.is_file()])
    print(f"dir: {dir.name} contains {fcount} files.")



RE: Failing to get Stat for a Directory - tester_V - Jul-20-2021

I like your thought!
But it would not work, there is a directory with one or two empty files.
That is why I tried to get Dir stat
 tt = pathlib.Path(ef).stat().st_size
as I said "I have directories with an empty file or two"


RE: Failing to get Stat for a Directory - bowlofred - Jul-20-2021

What do you mean by an "empty" file? A file with length zero? If so, search for that. Your code looks like it's checking the "size" of the directory, not the files within. The "size" of a directory isn't well defined. It depends on the specific filesystem and the history of the directory. It won't tell you anything about whether a file in that directory is "empty".

import pathlib
for ef in pathlib.Path('C:\\02').iterdir() :
    if ef.is_dir() :
        print(f" Directory -> {ef}")
        for file in ef.iterdir():
            if file.is_file() and file.stat().st_size == 0:
                print(f" file {file} is zero-length")
                # file.unlink()  # uncomment to remove the zero-length files.



RE: Failing to get Stat for a Directory - tester_V - Jul-20-2021

Yes, file size is "length zero",
I thought If file size is "0" then the Dir size is also "0" that is why I thought I'll check the Dir size.
for some reason the snipped below is working fine but if I'll do irerdir() then it fails:
import pathlib
st = pathlib.Path('C:\\02\\zero').stat().st_size
if st==0 :
    print(f" Empty-> {st}")
else :
    print(f" Directory is not Empty -->{st}")



RE: Failing to get Stat for a Directory - bowlofred - Jul-20-2021

Do you care about the files or the directories?

If you look at a file and it has stat.st_size == 0, then the file is "empty" or zero length. You can't get that information from looking at a directory.

If you look at a directory and it has stat.st_size == 0, then you can't really tell anything. While that's unusual for some filesystems, it is legal for others to have files inside (that may or may not be "empty").

Your code is looking at one specific path: "C:\02\zero". Is this a file or a directory? If it's a file, then the print in the else clause seems incorrect because you don't know anything about a directory. If it's a directory, then the st_size==0 doesn't tell you anything useful and you shouldn't rely on it.


RE: Failing to get Stat for a Directory - tester_V - Jul-20-2021

I'd like to find Directories that have size=0.
Some of the directories have files, empty files, files that have a size=0.
'C:\02\zero' is a subdirectory of "02" directory, it is my testing subdirectory and I called it 'zero'.
I was wondering if I can do that (find directories the size=0) with "pathlib.Path"
I'm iterating "C:\\02" and testing subdirectories if any of the subdirs has size=0.
import pathlib
for ef in pathlib.Path('C:\\02').iterdir() :
    if ef.is_dir() :
        print(f" Directory -> {ef}")
        tt = pathlib.Path(ef).stat().st_size  ##< trying to find the size of the SubDir
        print(f" Size -> {tt}")
        if tt==0 :
            print(f" Empty Dir -> {tt}")       
        else :
            print(f"Not Empty Dir ->{tt}")   
thank you!


RE: Failing to get Stat for a Directory - bowlofred - Jul-20-2021

(Jul-20-2021, 06:08 AM)tester_V Wrote: I'd like to find Directories that have size=0.

Why? What do you think that means? You can't use that to find directories that have no children. You can't use that to find directories that have empty files. So I don't think looking for it is useful.

Quote:Some of the directories have files, empty files, files that have a size=0.
Yes, but that is the file that has size==0, not the directory. You need to iterate in the directory and look for every file inside that has st_size==0. That's what my previous example does.

Quote:'C:\02\zero' is a subdirectory of "02" directory, it is my testing subdirectory and I called it 'zero'.
I was wondering if I can do that (find directories the size=0) with "pathlib.Path"

You can, but I don't see how it would be useful to do so.


RE: Failing to get Stat for a Directory - Larz60+ - Jul-20-2021

I modified the path of post 2 code that I gave you above so that it pointer to one of my directories.
from pathlib import Path

# set starting directory
# path = Path("C:\\02")
path = Path("./data")

dirs = [x for x in path.iterdir() if x.is_dir()]
for dir in dirs:
    fcount = len([x for x in dir.iterdir() if x.is_file()])
    print(f"dir: {dir.name} contains {fcount} files.")
Here's the results:
Output:
dir csv contains 1 files. dir database contains 0 files. dir excel contains 4 files. dir html contains 0 files. dir json contains 2 files. dir ldfiles contains 0 files. dir misc contains 0 files. dir PDF contains 3 files. dir pretty contains 1 files. dir text contains 0 files. dir tmp contains 0 files.
You can show only directories that have zero files with a slight modification:
from pathlib import Path

path = Path("./data")
# set starting directory
# path = Path("C:\\02")

dirs = [x for x in path.iterdir() if x.is_dir()]
for dir in dirs:
    fcount = len([x for x in dir.iterdir() if x.is_file()])
    if not fcount:
        print(f"dir {dir.name} contains {fcount} files.")
Output:
dir database contains 0 files. dir html contains 0 files. dir ldfiles contains 0 files. dir misc contains 0 files. dir text contains 0 files. dir tmp contains 0 files.



RE: Failing to get Stat for a Directory - tester_V - Jul-20-2021

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.
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.

I'm looking if a directory size is =0. I thought I could do that with 'pathil.Path'.
And I can if I test one directory:
from pathlib import Path 
st = Path('C:\\02').stat().st_size
if st==0 :
    print(f" Empty-> {st}")
else :
    print(f" Directory is not Empty -->{st}")
Print out is
Directory is not Empty -->8192
But if I use the same code to iterate over a directory and test each Subdirectory if the size is=0 then it fails.

for ef in pathlib.Path('C:\\02').iterdir() :
    if ef.is_dir() :
        print(f" Directory -> {ef}")
        tt = pathlib.Path(ef).stat().st_size
        print(f" Size -> {tt}")
        if tt==0 :
            print(f" Empty Dir -> {tt}")       
        else :
            print(f"Not Empty Dir ->{tt}")  
Print out :

 Directory -> C:\02\2_B
 Size -> 0
 Empty Dir -> 0
 Directory -> C:\02\And_ler
 Size -> 0
 Empty Dir -> 0
 Directory -> C:\02\a_APSME
 Size -> 0
 Empty Dir -> 0
 Directory -> C:\02\Nothing
 Size -> 0
 Empty Dir -> 0
 Directory -> C:\02\zero
 Size -> 0
 Empty Dir -> 0
 Directory -> C:\02\Zip-TST
 Size -> 0
 Empty Dir -> 0
Than you!