954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using "startswith" to define which files to process

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)
kungfubambi
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

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)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

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!

kungfubambi
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: