473,320 Members | 2,189 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,320 software developers and data experts.

Problem in directory working

I've written Unix's find-like function using recursive os.listdir()
call and generator expression. It seems that error happens in
generator expression.
Below codes do not work. walk.next() generates StopIteration.
When I replace 'yield' with 'print', it works fine.

Why this does not work?
I've had successively implemented unix-find using os.path.walk().
But I want to combine recursive call and generator expressions.

Any comments are welcome.
[snip]
import os

def find(path):
if not os.path.exists(path):
raise StopIteration
if os.path.isdir(path):
for l in os.listdir(path):
fullpath = os.sep.join((path, l))
find(fullpath)
else:
yield path

if __name__=='__main__':
#find('/home/myhome'); raise SystemExit()
walk = find('/home/myhome')
for i in walk:
print i.next()
Jul 18 '05 #1
1 1575
Gyoung-Yoon Noh wrote:
I've written Unix's find-like function using recursive os.listdir()
call and generator expression. It seems that error happens in
generator expression.
Below codes do not work. walk.next() generates StopIteration.
When I replace 'yield' with 'print', it works fine.

Why this does not work?
I've had successively implemented unix-find using os.path.walk().
But I want to combine recursive call and generator expressions.

Any comments are welcome.
[snip]
import os

def find(path):
if not os.path.exists(path):
raise StopIteration
if os.path.isdir(path):
for l in os.listdir(path):
fullpath = os.sep.join((path, l)) for p in find(fullpath):
yield p else:
yield path

if __name__=='__main__':
#find('/home/myhome'); raise SystemExit()
walk = find('/home/myhome')
for i in walk:
print i.next()


Just writing find(somepath) will create a new generator but only yield a
value when its next() method is called. This is done implicitly by the for
loop I inserted into your code. Do you know about 2.3's new os.walk()? It
would make a good building block for routines scanning the file system.

Peter
Jul 18 '05 #2

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

Similar topics

3
by: Ron L | last post by:
I have an application that I am working on with 2 other developers. The applciation uses Remoting for our calls to SQL Server. We have been developing and testing against our development machines...
4
by: qbproger | last post by:
I'm developing a plugin for some software. The previous version of the software didn't require a start in directory to be set. This allowed me to leave the working directory to the default in the...
5
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in...
4
by: Jeff | last post by:
I feel like I should have been able to figure this out but I can't seem to find any references on this topic. It seems like my current working directory is consistently a few directories up from...
17
by: djmob | last post by:
I am very new to .NET and Windows programming in general so please bare with me... I created a Windows service by deriving a class from ServiceBase and installing it via the 'sc create' ...
11
by: brianlanning | last post by:
We have a web application in 2003. The solution contains the project for the web application and five other projects for shared assemblies. We have two issues. The first is that we want to...
6
by: +86 | last post by:
i encountered this problem: "include('inc.php')" will work problely but "include('./inc.php') doesn't work .. both file_exists('inc.php') or file_exists('./inc.php') didn't return the right...
8
by: =?Utf-8?B?TWlrZSBSYW5k?= | last post by:
I am trying to get a list of files from a specified directory using the System.IO namespace classes, and then use that list as the datasource for a GridView. I have been able to do this...
15
by: mike | last post by:
Hi, I am very new to PHP I am having php.ini problems. I am working in XP with IIS and have php installed as an ISAPI. I am trying to change values in my php environment like 'upload_tmp_dir',...
10
by: Raheem | last post by:
Hello, I built a development version of a live website on my hosted account. However the development version is having problems with finding include files. After troubleshooting I was able to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.