Python Forum

Full Version: os.path.join - errors out
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I'm trying to use 'os.walk' to find all the subdirectories and files in the root dir.
In the first "for" loop, the code generates an error Confused if I use:
os.path.join(root, dirs)
to get rid of an error I'm using a workaround :
root+'\\'+dr
I understand 'workaround' is not a good thing to use.
Here is the code.
import os
p = 'c:\\02'

for root, dirs, files in os.walk(p):
    for dr in dirs :
        #r = os.path.join(root, dirs)
        #print ("Dir/Path -- ", r)
        dp = root+'\\'+dr
        print (dp)
    for file in files:
        rf = os.path.join(root, file)
        print ('Roor+file-->> ',rf)
Any help is appriciated!
Thank you.
(Nov-22-2020, 07:15 AM)tester_V Wrote: [ -> ]the code generates an error
Please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post

while doing this, compare how you use os.path.join and the workaround. Do you spot the difference (dirs vs dr)?
for root, dirs, files ...

you will get lists of dirs and files

os.path.join needs string not list
I see your point but I actually have a typo Cry in the code that is why it produced an error.
the code below works fine Dance .
import os
p = 'c:\\02'

for root, dirs, files in os.walk(p):
    for dr in dirs :
        r = os.path.join(root, dr)
        print ("Dir/Path -- ", r)
thank you for your help! I really appreciate it. Smile
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.