Hi, I'm pretty new to Python. Working in 2.4 due to company restraints. I'm trying to use os.path.walk to work through a tree directory, and perform different actions on the files based on the name of the file. I thought if I used "startwith" it would indicate that files only called "roads." would be processed. Unfortunatly it just goes ahead and processes all the files in the directories. I don't know a better way! Any help would be much appreciated.

The code has been simplified to just show this problem.

import sys, os

def treeDir (arg, dirpath, basenames):
    workspace = dirpath
    for f in basenames:
        if f.startswith('roads.'):
            fullf = os.path.join(dirpath,f)

    try:
        # This space is for some some processing commands

            

    ##        #Add A Completion Message
        print ("Well done.  Now you can move on to the Next Big Thing")

    except:
        print ("Something's wrong")

root = "C:\\scratch\\mine\\TEST"
os.path.walk(root,treeDir,None)

Recommended Answers

All 3 Replies

Just to simplify little and use better names.

import os

for root, dirs, files in os.walk(r'C:\test'):
    for f in files:
         if f.startswith('roads.'):
            print f
            #print os.path.join(root, f)

So this will print only files that starswith "roads" in directory c:\test.
we iterate over files and last line we can join root(path) with filename.

Everything should go under the if statement (indents count in Python), or copy the file you want to process to a list and process the list.

def treeDir (arg, dirpath, basenames):
    workspace = dirpath
    roads_list = []
    for f in basenames:
        if f.startswith('roads.'):
            fullf = os.path.join(dirpath,f)
            function_to_process_this_file(fullf)

            ##
            ## or
            roads_list.append(fullf)

    ## all files are processed
    for fname in roads_list:
        function_to_process_this_file(fname)

Hi, all,

Here's what I ended up with:

def treeDir (arg, dirpath, basenames):
    gp.workspace = dirpath
    lines_list = []
    for f in basenames:
         if f == "roads.shp":
            fullf = os.path.join(dirpath,f)
            print "file=" + fullf 
           # function_to_process_this_file(fullf)

I went with the equal comaprison beacause the function process can only work on the files with .shp extention. (If you've ever worked on shapefiles, you know what I mean.
:-)
Thanks for helping me get my head straight!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.