Opening files in a folder

windows
rhinopython

#1

Hi there,
fairly new to Rhino Python but I am already getting some results. I ran a useful script for a single model but I need to apply that same scripts to another 100 models within the same folder. How would I go about opening a model from a specific folder, do certain operations through the script, then proceed to the next model ?

Basically I wanna run through all the models in a folder and execute the same script for each one of them. Any suggestions on the right approach ?


(Pascal Golay) #2

Hi Gabriele - you can use rs.BrowseForFolder to get the folder from the user. Then Python has some ways of getting at the files and making a list of them, for example:

import rhinoscriptsyntax as rs
import os

def test():
    fol = rs.BrowseForFolder()
    listing  = os.listdir(fol)
    rFiles = []
    for file in listing:
        print os.path.splitext(file)[1]
        if os.path.splitext(file)[1] == ".3dm":
            rFiles.append(fol + "/" +file)  
    
    if len(rFiles) == 0: return 
    for rFile in rFiles:
        
        #do somehing
    
    
    
test()

-Pascal


#3

Thanks a lot pascal, I will try that soon. For some reason I thought I wouldn’t be able to import os in Rhino Python.