os.walk: Get entire path

F

Florian Lindner

Hello,
when I'm walking through a file system hierarchy using os.walk, how can I
get the full path of a file or dir? normpath and abspath don't work.
Thx,
Florian
 
P

Peter Kleiweg

Florian Lindner schreef:
Hello,
when I'm walking through a file system hierarchy using os.walk, how can I
get the full path of a file or dir? normpath and abspath don't work.


Sure it does.

os.path.abspath(os.path.join(root, file_or_dir))
 
T

Tom B.

Florian Lindner said:
Hello,
when I'm walking through a file system hierarchy using os.walk, how can I
get the full path of a file or dir? normpath and abspath don't work.
Thx,
Florian

I would use glob

import glob

glob.glob('c:\\images\\*\\*.jpg')

yields all .jpg's in //images and sub directories with full path in a list.

Tom
 
S

Steve Holden

Tom said:
I would use glob

import glob

glob.glob('c:\\images\\*\\*.jpg')

yields all .jpg's in //images and sub directories with full path in a list.

No, it doesn't.

If the directory hierarchy is more thane one level deep you won't see
those files, and neither (I believe without verifying) will you see
files from the top-level directory itself.

regards
Steve
 
M

Miki Tebeka

Hello Florian,
when I'm walking through a file system hierarchy using os.walk, how can I
get the full path of a file or dir? normpath and abspath don't work.
from os.path import join, abspath
from os import walk

for root, dirs, files in walk("somedir"):
for file in files:
print abspath(join(root, file))

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top