Python Forum
Failing to get Stat for a Directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to get Stat for a Directory
#1
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.
Reply
#2
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.")
tester_V likes this post
Reply
#3
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"
Reply
#4
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.
tester_V likes this post
Reply
#5
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}")
Reply
#6
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.
Reply
#7
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!
Reply
#8
(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.
Reply
#9
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.
Reply
#10
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 44 2 hours ago
Last Post: tester_V
  Failing regex tester_V 3 1,166 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Unknown Error with Random Stat Assigner FC8 6 2,303 Dec-06-2021, 01:56 PM
Last Post: FC8
  Failing to connect to a host with WMI tester_V 6 4,365 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to Zip files tester_V 4 2,145 Dec-01-2020, 07:28 AM
Last Post: tester_V
  Python 3.8 on mac failing to start sgandon 0 2,892 Jan-14-2020, 10:58 AM
Last Post: sgandon
  trying to install oandapy and failing ErnestTBass 0 1,894 Feb-24-2019, 06:13 PM
Last Post: ErnestTBass
  Pyinstaller failing JP_ROMANO 2 4,055 Jan-16-2019, 06:07 PM
Last Post: JP_ROMANO
  Why is my for loop failing? microphone_head 4 2,979 Sep-11-2018, 01:21 PM
Last Post: microphone_head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020