467,912 Members | 1,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,912 developers. It's quick & easy.

Just want to walk a single directory

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)
Jan 14 '06 #1
  • viewed: 999
Share:
3 Replies
[st*************@hotmail.com]
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))]
Jan 14 '06 #2
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.

Tim Peters <ti********@gmail.com> wrote in
news:ma**************************************@pyth on.org:
thebasedir = "C:\\tmpold"
for dummy, dummy, permanentFilelist in os.walk(thebasedir):
break


Jan 14 '06 #3
SB <st*************@hotmail.com> wrote:
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.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by hokieghal99 | last post: by
reply views Thread by Frans Englich | last post: by
7 posts views Thread by KraftDiner | last post: by
9 posts views Thread by silverburgh.meryl | last post: by
2 posts views Thread by gregpinero | last post: by
4 posts views Thread by Marcus Alves Grando | last post: by
4 posts views Thread by tdahsu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.