473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
3 1191
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: hokieghal99 | last post by:
This script is not recursive... in order to make it recursive, I have to call it several times (my kludge... hey, it works). I thought os.walk's sole purpose was to recursively walk a directory...
0
by: Frans Englich | last post by:
Hello, Have a look at this recursive function: def walkDirectory( directory, element ): element = element.newChild( None, "directory", None ) # automatically appends to parent...
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned... Can someone explain please? for root, dirs,...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
9
by: silverburgh.meryl | last post by:
i am trying to use python to walk thru each subdirectory from a top directory. Here is my script: savedPagesDirectory = "/home/meryl/saved_pages/data" dir=open(savedPagesDirectory, 'r') ...
2
by: gregpinero | last post by:
In the example from help(os.walk) it lists this: from os.path import join, getsize for root, dirs, files in walk('python/Lib/email'): print root, "consumes", print sum(), print "bytes in",...
4
by: Marcus Alves Grando | last post by:
Hello list, I have a strange problem with os.walk and threads in python script. I have one script that create some threads and consume Queue. For every value in Queue this script run os.walk()...
2
by: dj | last post by:
Hello All, I am attempting to us os.walk to populate two lists with values from a directory. The first list contains all the files in the directory and subdirectories. The second list contains...
4
by: tdahsu | last post by:
Hi, I'm using os.walk as follows: (basedir, pathnames, files) = os.walk("results", topdown=True) and I'm getting the error: ValueError: too many values to unpack
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.