473,503 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

os.walk() dirs and files

Hello,

When working with file and dir info recursively on Windows XP. I'm going
about it like this:

for root, dirs, files in os.walk(path):
for f in files:
ADD F to dictionary
for d in dirs:
ADD D to dictionary

Is it possible to do something such as this:

for root, dirs, files in os.walk(path):
for f,d in files, dirs:
ADD F|D to dictionary

Just trying to save some lines of code and thought it wise to ask the
gurus before trying it :)

Thanks!
Feb 8 '06 #1
6 2909
rtilley wrote:
Hello,

When working with file and dir info recursively on Windows XP. I'm going
about it like this:

for root, dirs, files in os.walk(path):
for f in files:
ADD F to dictionary
for d in dirs:
ADD D to dictionary

Is it possible to do something such as this:

for root, dirs, files in os.walk(path):
for f,d in files, dirs:
ADD F|D to dictionary


Just to clarify. In this particular case, I do not need to differentiate
between files and dirs... so would it be possible to do something such
as this:

for root, dirs, files in os.walk(path):
for fs_object in files, dirs:
ADD fs_object to dictionary
Feb 8 '06 #2
rtilley wrote:
Just to clarify. In this particular case, I do not need to differentiate
between files and dirs... so would it be possible to do something such
as this:

How about just concatentating the two lists:
for root, dirs, files in os.walk(path):
for fs_object in files, dirs: for fs_object in files + dirs: ADD fs_object to dictionary


Feb 8 '06 #3
Duncan Booth wrote:
How about just concatentating the two lists:
for root, dirs, files in os.walk(path):

for fs_object in files + dirs:
ADD fs_object to dictionary


Thank you Duncan! that solves the problem perfectly!
Feb 8 '06 #4
rtilley wrote:
Duncan Booth wrote:
How about just concatentating the two lists:
for root, dirs, files in os.walk(path):


for fs_object in files + dirs:
ADD fs_object to dictionary

Thank you Duncan! that solves the problem perfectly!


Or a bit more efficiently (no need to allocate a new list for storing
files+dirs):

from itertools import chain
for root, dirs, files in os.walk(path):
for fs_object in chain(files,dirs):
ADD fs_object to dictionary

Or you can download the path.py module
(http://www.jorendorff.com/articles/python/path/):

from path import path
for fs_object in path(root).walk():
ADD fs_object to dictionary
George

Feb 8 '06 #5
[rtilley]
When working with file and dir info recursively on Windows XP. I'm going
about it like this:

for root, dirs, files in os.walk(path):
for f in files:
ADD F to dictionary
for d in dirs:
ADD D to dictionary

Is it possible to do something such as this:

for root, dirs, files in os.walk(path):
for f,d in files, dirs:
ADD F|D to dictionary

Just trying to save some lines of code and thought it wise to ask the
gurus before trying it :)


As has been pointed out,

for name in dirs + files:

is simple and effective. In a context where you don't want to endure
the memory burden of materializing a concatentated list, you can do

for name in itertools.chain(dirs, files):

intead.
Feb 8 '06 #6
George Sakkis wrote:
Or a bit more efficiently (no need to allocate a new list for storing
files+dirs):

from itertools import chain
for root, dirs, files in os.walk(path):
for fs_object in chain(files,dirs):
ADD fs_object to dictionary


I like that! itertools is cool... a bit abstract and computer sciencey,
but it sure does work in a practical manner :)
Feb 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2896
by: Marcello Pietrobon | last post by:
Hello, I am using Pyton 2.3 I desire to walk a directory without recursion this only partly works: def walk_files() : for root, dirs, files in os.walk(top, topdown=True): for filename in...
5
3761
by: rbt | last post by:
Could someone demonstrate the correct/proper way to use os.walk() to skip certain files and folders while walking a specified path? I've read the module docs and googled to no avail and posted here...
7
4528
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,...
2
4013
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",...
0
7091
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
7342
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...
1
6998
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...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.