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! 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
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
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!
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
[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.
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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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,...
|
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",...
|
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...
| |
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...
|
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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |