Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.path.join - errors out
#1
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.
Reply
#2
(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)?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
for root, dirs, files ...

you will get lists of dirs and files

os.path.join needs string not list
Gribouillis likes this post
Reply
#4
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
Reply
#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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The difference between os.path.join( and os.sep.join( Pedroski55 2 1,087 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  os.path.join qmfoam 2 545 Nov-08-2020, 04:03 PM
Last Post: qmfoam
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 978 Feb-14-2019, 08:34 PM
Last Post: woooee
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 2,869 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