Python Forum
deleting an empty Dir, using pathlib.Path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deleting an empty Dir, using pathlib.Path
#1
Greetings!
I'm trying to use pathlib.Path to identify empty Subdirectories (and delete them).

For some reason, my code cannot identify if a Subdir is empty.
3 of 4 Subdirectories have a small text file inside but all 3 identified as Empty.
mydir = 'C:\\02'
for fel in pathlib.Path(mydir).iterdir():
    if fel.is_dir():
        print(f"{fel} is a Directory")
        p = pathlib.Path(fel)
        stat = p.stat().st_size
        print(f" Size = {stat}")
I tried adding IF but it did not do anygood...

mydir = 'C:\\02'
for fel in pathlib.Path(mydir).iterdir():
    if fel.is_dir():
        print(f"{fel} is a Directory")
        p = pathlib.Path(fel)
        stat = p.stat().st_size
        print(f" Size = {stat}")
        if stat == 0 :
            print(f" Empty Dir --> {stat}")  
Thank you.
Reply


Messages In This Thread
deleting an empty Dir, using pathlib.Path - by tester_V - Jun-30-2021, 11:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  -i option changes sys.path (removes leading empty string '') markanth 6 2,111 Aug-26-2022, 09:27 PM
Last Post: markanth
  Pathlib import not working chriswrcg 9 3,996 May-29-2022, 07:37 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,280 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Trying to pathlib instead of os.path tester_V 4 2,596 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,797 Dec-01-2020, 12:43 PM
Last Post: NaN
  Question about if with () or without () / pathlib Tecuma 3 2,309 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  pathlib hanging bluefrog 2 3,223 Sep-25-2018, 12:59 PM
Last Post: volcano63
  pathlib: resolving a path that does not exist Skaperen 6 5,671 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  makin hardlinks with pathlib.Path Skaperen 2 5,317 Sep-06-2018, 07:53 AM
Last Post: scidam
  Need help using pathlib to read text file into dictionary gwilli3 4 4,291 Aug-13-2018, 06:21 PM
Last Post: gwilli3

Forum Jump:

User Panel Messages

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