Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Just want to walk a single directory (http://www.velocityreviews.com/forums/t353425-just-want-to-walk-a-single-directory.html)

SB 01-14-2006 10:44 PM

Just want to walk a single directory
 
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)

Tim Peters 01-14-2006 11:01 PM

Re: Just want to walk a single directory
 
[stevebnospam428@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))]

SB 01-14-2006 11:25 PM

Re: Just want to walk a single directory
 
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 <tim.peters@gmail.com> wrote in
news:mailman.522.1137279686.27775.python-list@python.org:

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



Tim Roberts 01-15-2006 10:16 PM

Re: Just want to walk a single directory
 
SB <stevebnospam428@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, timr@probo.com
Providenza & Boekelheide, Inc.


All times are GMT. The time now is 06:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.