Just want to walk a single directory

S

SB

Hi,

I have a super-simple need to just walk the files in a single directory.

I thought this would do it, but "permanentFilelist" ends up containing
all folders in all subdirectories.

Could someone spot the problem? I've scoured some threads using XNews reg
expressions involving os.walk, but couldn't extrapolate the answer for my
need.

===============================================

thebasedir = "E:\\temp"

permanentFilelist= []

for thepath,thedirnames,thefilenames in os.walk(thebasedir):

if thepath != thebasedir:

thedirnames[:] = []

for names in thefilenames:

permanentFilelist.append(names)
 
T

Tim Peters

[[email protected]]
I have a super-simple need to just walk the files in a single directory.

I thought this would do it, but "permanentFilelist" ends up containing
all folders in all subdirectories.

All folders everywhere, or all file (not directory) names in the top
two levels? It looks like the latter to me.
Could someone spot the problem? I've scoured some threads using XNews reg
expressions involving os.walk, but couldn't extrapolate the answer for my
need.

===============================================

thebasedir = "E:\\temp"

permanentFilelist= []

for thepath,thedirnames,thefilenames in os.walk(thebasedir):

if thepath != thebasedir:

You wanted == instead of != there. Think about it ;-)
thedirnames[:] = []

for names in thefilenames:
permanentFilelist.append(names)

A simpler way (assuming I understand what you're after) is:

thebasedir = "C:\\tmpold"
for dummy, dummy, permanentFilelist in os.walk(thebasedir):
break

or the possibly more cryptic equivalent:

thebasedir = "C:\\tmpold"
permanentFilelist = os.walk(thebasedir).next()[-1]

or the wordier but transparent:

thebasedir = "C:\\tmpold"
permanentFilelist = [fn for fn in os.listdir(thebasedir)
if os.path.isfile(os.path.join(thebasedir, fn))]
 
S

SB

Thanks Tim, this definitely does it. I was trying to prevent having to
spend time absorbing the core of how generators work, because this simple
is all I need to do, and I don't have the updated python cookbook book. The
one I have discussed the old os.path.walk.
 
T

Tim Roberts

SB said:
Hi,

I have a super-simple need to just walk the files in a single directory.

I thought this would do it, but "permanentFilelist" ends up containing
all folders in all subdirectories.

Could someone spot the problem? I've scoured some threads using XNews reg
expressions involving os.walk, but couldn't extrapolate the answer for my
need.

===============================================

thebasedir = "E:\\temp"

permanentFilelist= []

for thepath,thedirnames,thefilenames in os.walk(thebasedir):
if thepath != thebasedir:
thedirnames[:] = []
for names in thefilenames:
permanentFilelist.append(names)

Um, excuse me for butting in, but couldn't you accomplish the same thing
more simply by using os.listdir and os.path.isfile? In my brain, os.walk
is the solution to RECURSIVE search needs.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top