Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.path.join - errors out
#5
Modern Python:

from pathlib import Path

data_path = Path('c:\\02')


for path in data_path.rglob("*"):
    if path.is_file():
        ...
    if path.is_dir():
        parent = path.parent
        name = path.name
        size_mib = path.stat().st_size / 1024**2
        if size_mib < 1:
            # skipping files with less than 1 MiB
            continue
        print(name, "->", size_mib)
The first party module pathlib has a better abstraction for paths.
So if you don't optimize to list 10_000_000 files, pathlib would do its job.

You should read this: https://realpython.com/python-pathlib/
The rglob is a recursive glob. It returns a generator and the generator yields Path objects. You can work with Path objects. For example you can get the parent (which is a dir, if the path was pointing to a file). You can get also the stat from files.
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
os.path.join - errors out - by tester_V - Nov-22-2020, 07:15 AM
RE: os.path.join - errors out - by buran - Nov-22-2020, 08:36 AM
RE: os.path.join - errors out - by Axel_Erfurt - Nov-22-2020, 09:30 AM
RE: os.path.join - errors out - by tester_V - Nov-29-2020, 04:44 AM
RE: os.path.join - errors out - by DeaD_EyE - Nov-29-2020, 08:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  os.path.join() 'NoneType' confusion gowb0w 11 1,574 Sep-22-2023, 11:13 PM
Last Post: deanhystad
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,215 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,472 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  os.path.join qmfoam 2 2,381 Nov-08-2020, 04:03 PM
Last Post: qmfoam
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,246 Feb-14-2019, 08:34 PM
Last Post: woooee
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,775 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

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